How to prevent MongoDB from returning the object ID when finding a document?
Asked Answered
O

5

28

I've the following document in MongoDB...

{
  "_id" : ObjectId("531221cd960100960116b992"),
  "username : "joe",
  "address" : [
    {
      "zip" : "8000",
      "city" : "Zurich"
    },
    {
      "zip" : "6900",
      "city" : "Lugano"
    }
  ]
}

... and to retrieve the second address I use the following statement:

db.users.find({ _id: ObjectId("531221cd960100960116b992") }, { addresses: { $slice: [0, 1] } } )

This works except it also returns the object id:

{ "addresses" : [ { "zip" : "6900", "city" : "Lugano" } ], "_id" : ObjectId("531221cd960100960116b992") }

How do I prevent MongoDB from returning the object id? I know I should provide a projection like _id : 0... but where should I put it in the expression above? I did a number of tries... but without success.

Thanks.

Osanna answered 1/3, 2014 at 22:19 Comment(0)
H
18

This is exactly the same as @hanleyhansen's answer, but just to let you know that you can use 0 interchangeably with false like:

db.users.find(
  { _id: ObjectId("531221cd960100960116b992")},
  { addresses: { $slice: [0, 1] } ,'_id': 0}
)
Harlequin answered 1/3, 2014 at 23:41 Comment(2)
Here is what I was looking for... and it works: db.users.find({ _id: ObjectId("531221cd960100960116b992")}, { addresses: { $slice: [0, 1] } ,'_id': 0, 'addresses': 1} ) - I missed addresses: 1 ;-)Osanna
How to restring on schema level ?Mccay
G
27

Pass {'_id': False} as a parameter to find()

db.users.find({ _id: ObjectId("531221cd960100960116b992")}, { addresses: { $slice: [0, 1] } ,'_id': False} )
Gruel answered 1/3, 2014 at 22:21 Comment(6)
it should be false lowercase ( or 0 )Roderick
@IlanFrumer thank you. Been working in Python all day!Gruel
addresses: { $slice: [0, 1] } should be paste in second parameter (projection), not into query parameter.Partible
Maybe I'm doing something wrong... but if I copy&paste your find() statement I get error: { "$err" : "invalid operator: $slice", "code" : 10068 }Osanna
No, now I get all the documents in full (i.e. they also include the fields outside "addresses"Osanna
@Osanna May be problem is that your document have address field, but in query you trying to use addresses?Partible
H
18

This is exactly the same as @hanleyhansen's answer, but just to let you know that you can use 0 interchangeably with false like:

db.users.find(
  { _id: ObjectId("531221cd960100960116b992")},
  { addresses: { $slice: [0, 1] } ,'_id': 0}
)
Harlequin answered 1/3, 2014 at 23:41 Comment(2)
Here is what I was looking for... and it works: db.users.find({ _id: ObjectId("531221cd960100960116b992")}, { addresses: { $slice: [0, 1] } ,'_id': 0, 'addresses': 1} ) - I missed addresses: 1 ;-)Osanna
How to restring on schema level ?Mccay
T
4

use this code

 db.yourCollectionName.find({},{_id:0})
Themselves answered 20/5, 2020 at 13:57 Comment(0)
A
0

For anyone using string values to limit returned data - Just pass -_id as one of the options:

User.find(
    { name: "John Doe" },
    "name photo bio -_id", // <-- "-_id"
    { limit: 25 },
    function (err, users) {
      return res.status(200).send({
        success: true,
        users,
      });
    }
  );
Adjunction answered 13/8, 2021 at 10:45 Comment(0)
L
0

If you don't want to explicitly create id for addresses that are inside an object. Then you can create a folder named as "Models", then export your JavaScript file inside that folder and set the default value of _id as false and you are all done:

const userSchema = new Schema({
    username: String,
    addresses: [
        {
            _id: false,
            location: String,
            city: String,
        }
    ]
});
Loganloganberry answered 28/12, 2023 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.