bashkovpd
bashkovpd

Reputation: 598

Remove _id from output and nested element in Mongo

With my .findOne function I'm getting response:

{
    "_id": "59955bb0cd522a00bcaab08c",
    "lang": "en",
    "__v": 0,
    "message_subsets": [
        {
            "identifier": "errors",
            "_id": "59955bb0cd522a00bcaab08d",
            "messages": [
                {
                    "identifier": "invalid",
                    "message_text": "$FIELD$ is invalid",
                    "_id": "59955bb0cd522a00bcaab091"
                },
                {
                    "identifier": "empty",
                    "message_text": "$FIELD$ is required",
                    "_id": "59955bb0cd522a00bcaab090"
                }
            ]
        }
    ]
}

Is there any way to exclude _id from output and its nested elements with some .findOne query parameters?

Upvotes: 0

Views: 1360

Answers (2)

Mithun
Mithun

Reputation: 153

Let's say the collection is Test

db.test.findOne({},{_id:0,"message_subsets.messages._id":0, "message_subsets._id":0 })

Upvotes: 1

johniejohnie
johniejohnie

Reputation: 523

I'm not 100% sure on this, but I think mongo always returns the ID in a find query, you could try:

Query.findOne({_id: queryId}).select("-_id")

I haven't tested this, and am not sure if this will work. But worth the try ;-)

Upvotes: 1

Related Questions