mongo查询-从对象数组中获取特定对象(其_id是已知的),但该数组也是文档列表的一部分

时间:2018-09-05 06:54:17

标签: database mongodb

假设我有_id->即消息ID,如何查询来自收件箱或发件箱的特定消息。

this is my route 
get("/getSpecificMessage/{Message_id}", (req, res) => {..}

我能做的是找到所有买方/经销商,然后浏览所有买方/经销商的收件箱/发件箱,然后使用_id查找消息,即message_id ->那我可以做得更好吗?

{
        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e1"),
        "address" : {
                "proper_address" : "sarai kale khan",
                "lat" : 28.58894,
                "long" : "77.25692"
        },
        "name" : "prashant",
        "password" : "jfalksdjlk;jasdl",
        "email" : "prashant@gmail.com",
        "inbox" : [
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e4"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "from" : "1@1.com",
                        "message" : "message_1"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e3"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "from" : "2@2.com",
                        "message" : "message_2"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e2"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "from" : "3@3.com",
                        "message" : "message_3"
                }
        ],
        "outbox" : [
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e7"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "to" : "1@1.com",
                        "message" : "message_4"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e6"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "to" : "1@1.com",
                        "message" : "message_5"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e5"),
                        "date" : ISODate("2018-09-04T23:03:41.627Z"),
                        "to" : "1@1.com",
                        "message" : "message_6"
                }
        ],
        "__v" : 0
}
{
        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e8"),
        "address" : {
                "proper_address" : "najafgarah",
                "lat" : 28.58894,
                "long" : "77.25692"
        },
        "name" : "rahul",
        "password" : "jkalsjdflasdl",
        "email" : "rahul@gmail.com",
        "inbox" : [
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083eb"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "from" : "1@1.com",
                        "message" : "message_1"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083ea"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "from" : "2@2.com",
                        "message" : "message_2"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083e9"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "from" : "3@3.com",
                        "message" : "message_3"
                }
        ],
        "outbox" : [
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083ee"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "to" : "1@1.com",
                        "message" : "message_4"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083ed"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "to" : "1@1.com",
                        "message" : "message_5"
                },
                {
                        "_id" : ObjectId("5b8f0f4de276dd1e0ff083ec"),
                        "date" : ISODate("2018-09-04T23:03:41.639Z"),
                        "to" : "1@1.com",
                        "message" : "message_6"
                }
        ],
        "__v" : 0
}

所以如果我有_id = 5b8f0f4de276dd1e0ff083ea并且想要

{
  "_id" : ObjectId("5b8f0f4de276dd1e0ff083ea"),
  "date" : ISODate("2018-09-04T23:03:41.639Z"),
  "from" : "2@2.com",
  "message" : "message_2"
}

2 个答案:

答案 0 :(得分:1)

我不确定是否要使用MongoDB脚本或您的应用程序语言(如果我误认为是Nodejs)来使用它

这是它在Mongo Shell脚本上的工作方式

db.MODEL.find( { _id: DOCUMENT_ID },
                     { inbox: { $elemMatch: { _id: MESSAGE_ID } } } )

Documentation is here

如果这不是您想要的,请更新您的信息并添加您使用的语言/框架

答案 1 :(得分:1)

这是您可以使用聚合框架执行的操作:

var oId = new ObjectId("5b8f0f4de276dd1e0ff083ea");

db.collection.aggregate({
    $match: { // can be skipped but I'd personally keep it because this will use an index on "input._id"/"output._id" (if there is one) to filter out irrelevant documents
        $or: [
            { "inbox": { $elemMatch: { "_id": oId } } },
            { "outbox": { $elemMatch: { "_id": oId } } },
        ]
    }
}, {
    $project: {
        result: {
            $concatArrays: [
                { $filter: { "input": "$inbox", "cond": { $eq: [ "$$this._id", oId ] } } },
                { $filter: { "input": "$outbox", "cond": { $eq: [ "$$this._id", oId ] } } }
            ]
        }
    }
}, {
    $unwind: "$result" // flatten the result array
}, {
    $replaceRoot: {
        "newRoot": "$result" // move "result" contents all the way up
    }
})