Home > other >  How do I get the output field as an array in MongoDb
How do I get the output field as an array in MongoDb

Time:12-05

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")
  }
]

CodePudding user response:

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")
    ]
  }
]
  • Related