【问题标题】:Finding objects in nested arrays in MongoDB Realm在 MongoDB Realm 的嵌套数组中查找对象
【发布时间】:2021-08-01 01:58:13
【问题描述】:

使用 MongoDB Realm,我想找到嵌套在多个数组中的特定字段。文档结构如下:

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

我想查找属于位于library 的特定图书(带有唯一isbn)的注释(带有唯一_noteID)。

为了澄清,图书馆是一个包含书籍对象的数组,而书籍本身有一个笔记对象数组。

我在为此操作编写查询时遇到问题。我尝试了以下查询,但是这会返回属于这本书的所有笔记,而不是我根据其_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",
            }     
         ]    
       }

如何添加过滤器以便只收到我正在寻找的笔记?

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

【问题讨论】:

  • 最好用编码平台标记问题,以便其他具有该专业知识的用户看到它。

标签: javascript mongodb mongodb-query realm


【解决方案1】:

你应该试试这个,我认为这个解决方案会对你有所帮助。 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-aggregation-query.json

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

结果.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

查询.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
        }
    }
])

结果.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")
}

【讨论】:

  • 谢谢!结果有点不同,但我能够通过结合你的两种方法来弄清楚。
猜你喜欢
  • 2020-01-24
  • 2016-03-05
  • 2018-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
  • 2022-11-11
  • 1970-01-01
相关资源
最近更新 更多