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

时间:2021-12-24 16:52:58

how can I query to get the specific message from either inbox or outbox given that I have its _id --> i.e. message id.

如果我有_id - > ie消息ID,我如何查询从收件箱或发件箱中获取特定消息。

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

what I can do is find all the buyer/dealer and then go through inbox/outbox of all the buyer/dealer and then find the message with _id i.e. message_id --> can I do it better then that.

我能做的就是找到所有买家/经销商,然后通过所有买家/经销商的收件箱/发件箱,然后找到带有_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
}

So if I have the _id = 5b8f0f4de276dd1e0ff083ea and I want

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

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

2 个解决方案

#1


1  

I am not sure if you want it using MongoDB scripts or your app language (Nodejs if I am mistaken)

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

This is how it works on Mongo Shell Script

这是它在Mongo Shell Script上的工作原理

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

Documentation is here

文档在这里

If this is not what you want, please update your post and add which language/framework you are using

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

#2


1  

This is what you could do using the aggregation framework:

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

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

#1


1  

I am not sure if you want it using MongoDB scripts or your app language (Nodejs if I am mistaken)

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

This is how it works on Mongo Shell Script

这是它在Mongo Shell Script上的工作原理

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

Documentation is here

文档在这里

If this is not what you want, please update your post and add which language/framework you are using

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

#2


1  

This is what you could do using the aggregation framework:

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

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