Arrays 从id';猫鼬
我目前有一个引用子文档的多个id的数组。我需要做的是搜索这些Id的集合,并返回与它们相关的信息Arrays 从id';猫鼬,arrays,node.js,express,mongoose,subdocument,Arrays,Node.js,Express,Mongoose,Subdocument,我目前有一个引用子文档的多个id的数组。我需要做的是搜索这些Id的集合,并返回与它们相关的信息 var ids =['58c2871414cd3d209abf4fc0','58c2871414cd3d209abf5fc0']; User.aggregate( {$unwind : "$position"}, {$match:{'_id': { $in: [ // need to reference the ids array here ]} } },
var ids =['58c2871414cd3d209abf4fc0','58c2871414cd3d209abf5fc0'];
User.aggregate(
{$unwind : "$position"},
{$match:{'_id': { $in: [
// need to reference the ids array here
]}
}
}, function(err, docs){
console.log(docs);
});
});
我的用户模式如下所示:
var User = new Schema({
id: String,
name: String,
position: [{
_id:String,
title: String,
location: String,
start: String,
term:Number,
description:String,
date: {type: Date, default: Date.now},
所以基本上我需要做的是返回整个Position子文档,如果它与数组中的id匹配。有什么想法吗?您的mongo服务器版本是什么?您是否在单用户文档中搜索位置?@Veeram my mongo版本为v3.2.10。我有多个用户(用户模式),在这些用户中有不同的位置子文档(所有的位置子文档都有各自的id与之关联),我需要做的是搜索所有用户,展开位置子文档,并返回与id数组中id匹配的位置子文档I haveTry
user.aggregate({$unwind:“$position”},{$match:{“position.”{$id:{$in:ids}},{$project:{“position”:1}})
@Veeram我不幸地从代码中得到了一个空数组:(看起来聚合无法将id从字符串转换为objectID。请尝试将\u id
转换为变量id=[mongoose.Types.ObjectId('58c2871414cd3d209abf4fc0')、mongoose.Types.ObjectId('58c2871414cd3d209abf5fc0');
发送到$match