0

how can I query to get the specific message from either inbox or outbox given that I have its _id --> i.e. message 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" : 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" : ObjectId("5b8f0f4de276dd1e0ff083ea"),
  "date" : ISODate("2018-09-04T23:03:41.639Z"),
  "from" : "2@2.com",
  "message" : "message_2"
}
user6202188
  • 540
  • 1
  • 6
  • 13

2 Answers2

1

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

This is how it works on 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

Anouar Kacem
  • 635
  • 10
  • 24
  • can you help me with this question **https://stackoverflow.com/questions/52174001/query-mongo-find-count-of-array-in-all-the-documents-of-a-collection/52174911?noredirect=1#comment91307976_52174911** – user6202188 Sep 05 '18 at 08:18
  • @user6202188 you are welcome :) would please mark it as the right answer ? i'll check it – Anouar Kacem Sep 05 '18 at 08:19
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
    }
})
dnickless
  • 10,733
  • 1
  • 19
  • 34