在 MongoDB 领域的嵌套数组中查找对象

Finding objects in nested arrays in MongoDB Realm

使用 MongoDB 领域,我想找到一个嵌套在多个数组中的特定字段。该文档具有以下结构:

{
    "_id": "05932rkshfkj",
    "email": "user@email.com",
    "library": [
       {
        "isbn": "127395729387",
        "notes": [
            {
                "_noteID": "5tk3hk42jg2kgawef",
                "noteTitle": "title",
            },
            {
                "_noteID": "8hrwefjhwefwaef",
                "noteTitle": "another title",
            }     
         ]    
       }
     ],
    "toRead": []
}

我想找到属于位于 library.[=18= 的特定书籍(具有唯一 isbn)的笔记(具有唯一 _noteID) ]

澄清一下,图书馆是一个保存书籍对象的数组,而书籍本身有一个笔记对象数组。

我在为此操作编写查询时遇到问题。我尝试了以下查询,但是 returns 所有属于这本书的笔记,而不是我正在寻找的基于其 _noteID:

的笔记
exports = async function(payload, response) {
  
  const {user, bookID, noteID} = payload.query;
  
  let collection = context.services.get("mongodb-atlas").db("myApp").collection("Users"); 
  
    let note = collection.findOne({
    email: user,
    "library.isbn" : `${bookID}`,
    "library.notes" : {$elemMatch: {"_noteID" : noteID}}},  {"library.notes.$": 1}
  )

  return note;
  
}

如果我尝试查询第二个音符,结果显示如下:

    "notes": [
            {
                "_noteID": "5tk3hk42jg2kgawef",
                "noteTitle": "title",
            },
            {
                "_noteID": "8hrwefjhwefwaef",
                "noteTitle": "another title",
            }     
         ]    
       }

如何添加过滤器以便只收到我要查找的便条?

我在这里错过了什么?为什么这不起作用?非常感谢您的意见。

你应该试试这个,我想这个解决方案会对你有所帮助。 users.collection.json

/* 1 */
{
    "_id" : "05932rkshfkj",
    "email" : "user@email.com",
    "library" : [ 
        {
            "isbn" : "127395729387",
            "notes" : [ 
                {
                    "_noteID" : "5tk3hk42jg2kgawef",
                    "noteTitle" : "title"
                }, 
                {
                    "_noteID" : "8hrwefjhwefwaef",
                    "noteTitle" : "another title"
                }
            ]
        }
    ],
    "toRead" : [],
    "createdAt" : ISODate("2021-08-01T04:12:13.733Z")
}

Mongo-聚合-query.json

db.users.aggregate([{
  "$match": {
    "$and": [{
      "_id": "05932rkshfkj"
    }, {
      "library.isbn": "127395729387"
    }]
  }
}, {
  "$unwind": "$library"
}, {
  "$unwind": "$library.notes"
}, {
  "$match": {
    "library.notes._noteID": "8hrwefjhwefwaef"
  }
}])

result.json

/* 1 */

{
    "_id" : "05932rkshfkj",
    "email" : "user@email.com",
    "library" : {
        "isbn" : "127395729387",
        "notes" : {
            "_noteID" : "8hrwefjhwefwaef",
            "noteTitle" : "another title"
        }
    },
    "toRead" : [],
    "createdAt" : ISODate("2021-08-01T04:12:13.733Z")
}

您也可以使用 $filter

Query.json

var email = "user@email.com";
var bookID = "127395729387";
var noteID = "8hrwefjhwefwaef";
db.users.aggregate([{
        "$match": {
            "$and": [{
                "email": email
            }, {
                "library.isbn": bookID
            }]
        }
    },
    {
        "$unwind": "$library"
    },
    {
        "$project": {
            "email": 1,
            "library.isbn": 1,
            "library.notes": {
                "$filter": {
                    input: "$library.notes",
                    as: "item",
                    cond: {
                        $eq: ["$$item._noteID", noteID]
                    }
                }
            },
            "toRead": 1,
            "createdAt": 1
        }
    }
])

Result.json

/* 1 */
{
    "_id" : "05932rkshfkj",
    "email" : "user@email.com",
    "library" : {
        "isbn" : "127395729387",
        "notes" : [ 
            {
                "_noteID" : "8hrwefjhwefwaef",
                "noteTitle" : "another title"
            }
        ]
    },
    "toRead" : [],
    "createdAt" : ISODate("2021-08-01T04:12:13.733Z")
}