在匹配数组的一个字段时返回整个文档



我有一个这样的集合:

db.collection1 = 
{
    "_id" : ObjectId(),
    "array" : [
        {
            "userid" : ObjectId(),
            "subfield1" : string,
            "subfield2" : number,
            "subfield3" : number,
            "subfield4" : Date
        },...
    ],
    "field1" : date,
    "field2" : date,
    "field3" : date,
    "field4" : date,
    "field5" : number,
    "field6" : [],
    "field7" : String,
    "field8" : {}
},...

现在,我使用了一个基于数组中userid字段的find查询,它有点像这样:

db.collection1.find({array:{"$elemMatch":{userid:uId}})

但我还想要的是,在检索记录之前,它还应该获取特定的数组元素,并将其作为返回对象的属性附加。

我对此的第二个查询是:

db.collection1.find({'array.userId':uId},{'array.$':1})

但我希望将第二次查询的结果附加为第一次查询中返回的对象的属性

我希望我清楚。。。。。

OK

我的预测是这样的:

{array.subfield2:0,array.subfield3:0,field2:0,field6:0,field7:0}

所以这就是我想要的结果。。。支持有3个用户,第三个用户与我传递的userId匹配。检索到的文档应该是这样的。

{
 _id: ObjectId(),
 array:[
 {
  userid:ObjectId(1),
  subfield1:string,
  subfield4:Date
 },
 {
  userid:ObjectId(2),
  subfield1:string,
  subfield4:Date
 },
 {
  userid:ObjectId(3),
  subfield1:string,
  subfield4:Date
 }],
 "field1" : date,
 "field3" : date,
 "field4" : date,
 "field5" : number,
 "field8" : {},
 DynamicField://This is the userobj of the 3rd usr which matches. Whole & intact 
 {
   "userid" : ObjectId(3),
   "subfield1" : string,
   "subfield2" : number,
   "subfield3" : number,
   "subfield4" : Date
 }
}

我想很多人都被你的"追加"术语弄糊涂了,但如果我读到你所说的话,你似乎希望返回"整个文档",但只包括数组的"匹配元素"。

因此,就像所有通用的"字段选择"技术一样,您必须非常具体地使用MongoDB,并在投影中包含所有字段名称:

db.collection1.find(
    { "array.userid": uId },   // Don't need elemMatch with one field
    {
        "array.$": 1,
        "field1": 1,
        "field2": 1,
        "field3": 1,
        "field4": 1,
        "field5": 1,
        "field6": 1,
        "field7": 1,
        "field8": 1,
    }
)

除非明确排除,否则_id始终被包含,并且您不能在此处"混合"其他包含/排除,这并不是说它有多大意义,因为您需要数组中匹配的位置。

编辑:我不知道你为什么要这样做,对我来说,上面的表格要好得多:

 db.collection1.aggregate([
     // Filter the matching document, as you do not want to do the rest of the
     // steps over the whole collection
     { "$match": {
         "array.userid": uId,   // Don't need elemMatch with one field
     }},
     // Store the original document, and a copy of the array
     { "$project": {
         "array": 1,
         "field1": 1,
         "field3": 1,
         "field4": 1,
         "field5": 1,
         "field8": 1,
         "dynamicField": "$array"
     }},
     // Unwind the array
     { "$unwind": "$dynamicField" },
     // Just match the field you want
     { "$match": { "dynamicField.userid": uId } },
     // Play with the other array now
     { "$unwind": "$array" },
     // Change the projection
     { "$project": {
         "array.userId": "$array.userId",
         "array.subfield1": "$array.subfield1", 
         "array.subfield4": "$array.subfield4", 
         "field1": 1,
         "field2": 1,
         "field3": 1,
         "field4": 1,
         "field5": 1,
         "field6": 1,
         "field7": 1,
         "field8": 1,
         "dynamicField": 1
     }},
     // Group back the array
     { "$group": {
         "_id": "$_id",
         "array": { "$push": "$array" },
         "field1": { "$first": "$field1" },
         "field3": { "$first": "$field3" },
         "field4": { "$first": "$field4" },
         "field5": { "$first": "$field5" },
         "field8": { "$first": "$field8" },
         "dynamicField": { "$first": "$dynamicField" }
     }}
 ])

相关内容

  • 没有找到相关文章

最新更新