Home > Enterprise >  Mongodb query object without field
Mongodb query object without field

Time:06-13

I'm trying to query a full mongodb field but without an intern field, like this document example:

{
  _id: ObjectId('x8234728x8z8381'),
  Example: {
       field1: 'first',
       field2: 'second',
       field3: {
          field 3.1: 'third'
       }
  }
}

enter image description here

I would like to return all "Example" field, but without "$field2". Is it possible in mongodb?

CodePudding user response:

db.collection.aggregate({
  "$project": {
    "Example.field2": 0
  }
})

You just ignore it in projection.

CodePudding user response:

You can use find and then just not "project" the fields you want to eliminate.

db.collection.find({
  "_id": ObjectId("62a6144faf59381072fa83d6")
},
{
  "Example.field2": false
})

Try it on mongoplayground.net.

  • Related