0

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?

3
  • Ouch! You named your fields with a $? It's doable, but why use $ and then have to worry about possible consequences? Commented Jun 12, 2022 at 16:40
  • It was just an example to mock my real data hahaha, when I put this data I didn't pay attention to it ahahah Commented Jun 12, 2022 at 16:43
  • It's also better not to use images for code/errors/back traces/stack traces/etc. If you put them in a code block, it's easier for us to copy/paste. Code images are like answer repellent. :-) Commented Jun 12, 2022 at 16:46

2 Answers 2

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

You just ignore it in projection.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.