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

Database mongo查询-从对象数组中获取特定对象(其`_id`已知),但该数组也是文档列表的一部分,database,mongodb,Database,Mongodb,如果我有其\u id-->即消息id,如何查询以从收件箱或发件箱获取特定消息 this is my route get("/getSpecificMessage/{Message_id}", (req, res) => {..} 我能做的是找到所有买家/经销商,然后查看所有买家/经销商的收件箱/发件箱,然后找到带有\u id的邮件,即邮件id -->我能做得更好吗 { "_id" : ObjectId("5b8f0f4de276dd1e0ff083e1"),

如果我有其
\u id
-->即消息id,如何查询以从收件箱或发件箱获取特定消息

this is my route 
get("/getSpecificMessage/{Message_id}", (req, res) => {..}
我能做的是找到所有买家/经销商,然后查看所有买家/经销商的收件箱/发件箱,然后找到带有
\u id
的邮件,即邮件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
}
因此,如果我有
\u id=5b8f0f4de276dd1e0ff083ea
,我想要

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

我不确定您是否希望使用MongoDB脚本或您的应用程序语言(如果我错了,请使用Nodejs)

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

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


如果这不是您想要的,请更新您的帖子并添加您正在使用的语言/框架

我不确定您是否希望使用MongoDB脚本或应用程序语言(如果我弄错了,请使用Nodejs)

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

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


如果这不是您想要的,请更新您的帖子并添加您正在使用的语言/框架

这是您可以使用聚合框架做的:

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
    }
})

这是使用聚合框架可以做到的:

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
    }
})

你能帮我回答这个问题吗@user620218不客气:)请把它标记为正确答案好吗?我会检查的,你能帮我回答这个问题吗@user620218不客气:)请把它标记为正确答案好吗?我去查一下