0

How do I get the output field as an array.

What I currently have:

db.users.find({ taskIds: { $in: [ObjectId("638764689c4ed1fbff18859a")] } }, {taskIds:1})

The output of it:

[
  {
    _id: ObjectId("638764689c4ed1fbff1885a2"),
    taskIds: [
      ObjectId("638764689c4ed1fbff18859a"),
      ObjectId("638764689c4ed1fbff18859c")
    ]
  }
]

The output I want:

[
  {
     ObjectId("638764689c4ed1fbff18859a"),
     ObjectId("638764689c4ed1fbff18859c")
  }
]
1
  • 4
    The output you want is not a valid BSON/JSON. An object should have key-value pair. I believe what you want is an array of ObjectId(s). Commented Dec 3, 2022 at 15:09

1 Answer 1

1

If it's just to omit the _id from the result, you need to add it to your projection as follows: db.users.find({ taskIds: { $in: [ObjectId("638764689c4ed1fbff18859a")] } }, {taskIds:1, _id:-1})

This would produce:

[
  {
    taskIds: [
      ObjectId("638764689c4ed1fbff18859a"),
      ObjectId("638764689c4ed1fbff18859c")
    ]
  }
]
Sign up to request clarification or add additional context in comments.

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.