gpt4 book ai didi

javascript - 有没有办法找到匹配两个不同填充的文档并在 findOne() 中获取他的文档?

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

我将 mongoose 与组合 mongoDb/nodejs 结合使用。我想findOne() 一个有一些条件的文档。

这是我的架构:

var prognosticSchema = new Schema({
userRef : { type : Schema.Types.ObjectId, ref : 'users'},
matchRef : { type : Schema.Types.ObjectId, ref : 'match'},
...
});

模型架构'users'包含一个字符串'email',模型'match'包含一个数字'id_match' 像这样:

var userSchema = new Schema({
email: String,
...
});

然后

var matchSchema = new Schema({
id_match: {type: Number, min: 1, max: 51},
...
});

我的目标是findOne() 一个包含id_match = id_matchemail = req.headers['x-key']

我试过这个:

var prognoSchema = require('../db_schema/prognostic'); // require prognostics
require('../db_schema/match'); // require match to be able to populate

var prognoQuery = prognoSchema.find()
.populate({path: 'userRef', // populate userRef
match : {
'email' : req.headers['x-key'] // populate where email match with email in headers of request (I'm using Express as node module)
},
select : 'email pseudo'
});

prognoQuery.findOne() // search for only one doc
.populate({path: 'matchRef', // populate match
match: {
'id_match': id_match // populate match where id_match is correct
}})
.exec(function(err, data) {
... // Return of value as response ...
}

当我运行此代码并尝试获取正确的文档时,我知道其中有很多其他 prognosticSchema 以及其他 usersmatch我的数据库,我将在我的数据文档中获取 null 的 userRef 并更正 ma​​tchRef

在我的数据库中,还有其他 users 和其他 id_match 但我想在 findOne() 帮助下获得正确的文档这两个 objectId 在我的 Schema 中。

有没有办法findOne()一个文档匹配两个不同的populates并在findOne()中得到他的文档?

最佳答案

好吧,您可以在同一个查询中包含“两个”populate 表达式,但是当然,由于您实际上想要“匹配”包含在“引用”集合中的属性,这确实意味着实际从“父”返回的数据需要先查看“所有父”才能填充数据:

prognoSchema.find()
.populate([
{
"path": "userRef",
"match": { "email": req.headers['x-key'] }
},
{
"path": "matchRef",
"match": { "id_match": id_match }
}
]).exec(function(err,data) {
/*
data contains the whole collection since there was no
condition there. But populated references that did not
match are now null. So .filter() them:
*/
data = data.filter(function(doc) {
return ( doc.userRef != null && doc.matchRef != null );
});

// data now contains only those item(s) that matched
})

这并不理想,但这正是使用“引用”数据的方式。

更好的方法是“单独”搜索其他集合以找​​到单个匹配项,然后将找到的 _id 值提供给“父”集合。来自async.parallel的一点帮助此处是为了方便在使用匹配值在父级上执行之前等待其他查询的结果。可以通过多种方式完成,但这看起来比较干净:

async.parallel(
{
"userRef": function(callback) {
User.findOne({ "email": req.headers['x-key'] },callback);
},
"id_match": function(callback) {
Match.findOne({ "id_match": id_match },callback);
}
},
function(err,result) {
prognoSchema.findOne({
"userRef": result.userRef._id,
"matchRef": result.id_match._id
}).populate([
{ "path": "userRef", "match": { "email": req.headers['x-key'] } },
{ "path": "matchRef", "match": { "id_match": id_match } }
]).exec(function(err,progno) {
// Matched and populated data only
})
}
)

作为替代方案,在 3.2 及更高版本的现代 MongoDB 版本中,您可以使用 $lookup聚合运算符代替:

prognoSchema.aggregate(
[
// $lookup the userRef data
{ "$lookup": {
"from": "users",
"localField": "userRef",
"foreignField": "_id",
"as": "userRef"
}},
// target is an array always so $unwind
{ "$unwind": "$userRef" },
// Then filter out anything that does not match
{ "$match": {
"userRef.email": req.headers['x-key']
}},
// $lookup the matchRef data
{ "$lookup": {
"from": "matches",
"localField": "matchRef",
"foreignField": "_id",
"as": "matchRef"
}},
// target is an array always so $unwind
{ "$unwind": "$matchRef" },
// Then filter out anything that does not match
{ "$match": {
"matchRef.id_match": id_match
}}
],
function(err,prognos) {

}
)

但同样丑陋,因为“来源”仍在选择所有内容,而您只是在每个 $lookup 之后逐渐过滤掉结果操作。

这里的基本前提是“MongoDB 不‘真正’执行连接”.populate() 也不是“JOIN ",但只是对相关集合的额外查询。由于这“不是”“连接”,因此在检索到实际相关数据之前无法过滤掉“父”。即使它是通过 $lookup 在“服务器”上完成的而不是通过 .populate()

在“客户端”上

因此,如果您“必须”以这种方式查询,通常最好“首先”查询其他集合的结果,然后根据匹配的 _id 属性值作为引用来匹配“parent”。

但这里的另一种情况是您“应该”考虑“嵌入”数据,而您的意图是“查询”这些属性。 只有当数据驻留在“单个集合”中时,MongoDB 才可能通过单个查询和高性能操作来查询和匹配这些条件。

关于javascript - 有没有办法找到匹配两个不同填充的文档并在 findOne() 中获取他的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659337/

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