gpt4 book ai didi

node.js - mongoDB:聚合 - 是否有等效于 native node.js 驱动程序的 $lookup 连接?

转载 作者:可可西里 更新时间:2023-11-01 09:35:25 25 4
gpt4 key购买 nike

mongodb: 2.1.3

阅读了一些 aggregation enhancements 之后来到 mongoDB 3.2 我对“$look”管道阶段进行左外等值连接感到兴奋。

不幸的是, Node 驱动程序似乎没有此运算符。(我在 native driver docs for node 中没有看到它,当我尝试使用它时出现错误:

更新:这是我试过的代码

var cursor = db.collection('messagethreads').aggregate([
{"$match": {
_id: new ObjectID(threadID)}
},
{"$lookup": {
from: "messages",
localField: "_id",
foreignField: "threadID",
as: "messagesList"}
}
]);

cursor.toArray(function(err,messages){
if(err) {
res.status(500).json(error);
}
else if(convo === null){
res.status(400).end();
}
else{
res.status(200).json(messages);
}
});
});

示例 - ThreadMessage 文档

{ 
"_id" : ObjectId("56b4f52c0e6368c00630aee6"),
name: "Messages 1"
}

示例 - 消息文档

{
"_id" : ObjectId("56b4f52c0e6368c00630af08"),
"author" : "Nick",
"text" : "Hello",
"threadID" : ObjectId("56b4f52c0e6368c00630aee6")
},
...

预期结果

{
"_id" : ObjectId("56b4f52c0e6368c00630aee6"),
name: "Messages 1",
messageList:[
{
"_id" : ObjectId("56b4f52c0e6368c00630af08"),
"author" : "Nick",
"text" : "Hello",
"threadID" : ObjectId("56b4f52c0e6368c00630aee6")
},
...
]
}

"MongoError: exception: Unrecognized pipeline stage name: '$lookup' "

您可以阅读更多关于连接案例的信息 here !

问题:是否有预期的方法来执行与 node.js native 驱动程序等效的操作?

最佳答案

“MongoError”异常是驱动程序报告来自“服务器”的任何错误消息的方式,因此此类错误表明连接到的服务器不是支持$lookup 的版本。 , 为 3.2 或更高:

$lookup

New in version 3.2.

Performs a left outer join to an unsharded collection in the same database to filter in documents from the “joined” collection for processing. The $lookup stage does an equality match between a field from the input documents with a field from the documents of the “joined” collection.

您始终可以通过 serverStatus 获取要连接的服务器版本数据库命令。同样在完整的可复制 list 中:

var async = require('async'),
mongodb = require('mongodb'),
MongoClient = mongodb.MongoClient,
ObjectId = mongodb.ObjectId;

MongoClient.connect("mongodb://localhost/test",function(err,db) {

async.series(
[
function(callback) {
db.command({ "serverStatus": 1 }, function(err,status) {
console.log(status.version);
callback(err);
});
},
function(callback) {
async.each(['threadmessage','message'],function(colname,callback) {
db.collection(colname).remove({},callback);
},callback);
},

function(callback) {
db.collection('threadmessage').insert(
{
"_id" : ObjectId("56b4f52c0e6368c00630aee6"),
"name": "Messages 1"
},
callback
);
},

function(callback) {
db.collection('message').insert(
{
"_id" : ObjectId("56b4f52c0e6368c00630af08"),
"author" : "Nick",
"text" : "Hello",
"threadID" : ObjectId("56b4f52c0e6368c00630aee6")
},
callback
);
},

function(callback) {

var cursor = db.collection('threadmessage').aggregate([
{ "$lookup": {
"from": "message",
"localField": "_id",
"foreignField": "threadID",
"as": "messagesList"
}}
]);

cursor.toArray(function(err,result) {
console.log(JSON.stringify(result,undefined,2));
callback(err);
});
}
],
function(err) {
if (err) throw err;
db.close();
}
);

});

以及固定了驱动程序版本的 package.json,只是为了表明没有驱动程序版本问题:

{
"name": "lookup",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"async": "^1.5.2",
"mongodb": "2.1.3"
}
}

使用受支持的服务器版本提供预期的输出:

3.2.0
[
{
"_id": "56b4f52c0e6368c00630aee6",
"name": "Messages 1",
"messagesList": [
{
"_id": "56b4f52c0e6368c00630af08",
"author": "Nick",
"text": "Hello",
"threadID": "56b4f52c0e6368c00630aee6"
}
]
}
]

因此,如果该 list 未在您连接到的数据库上返回 3.2.x,则此处不支持 $lookup 管道操作,您将不得不诉诸替代手段,例如改为“客户端”拉取“相关”信息。

关于node.js - mongoDB:聚合 - 是否有等效于 native node.js 驱动程序的 $lookup 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com