0

I would like to query data from mongodb and filter based on some condition. The idea is let say I would like to receive all items where status is sold

Code looks as below:

db.collection('items').find({ users.name: { $in: ["Tomas","Antti"] } }).toArray(function (err, items) {
    console.log(items);
});

and my collection looks like:

{
  _id: ObjectId("5c9cddd50e08a84cd3f4cccb"),
  item: "Item 1",
  users: { 
    "be81afc": {"name:"John"},
    "78sdjh3": {"name:"Antti"},
    "78sd78a": {"name:"Antti"},
  },
  timestamp: 123123123
}

Supposing that I have many documents on items collection, how to filter items on getting items where the condition is met using where statement. As you can see there is a hash code within users which I dont need to care about, all I need is filter items where users name is equal to for ex Tomas and Antti

Mizlul
  • 1
  • 7
  • 41
  • 100
  • I cant change the schema, that's how is it coming from other party service! – Mizlul Apr 03 '19 at 12:45
  • In that case use the **MongoDB 3.6 Update** section [here](https://stackoverflow.com/a/19802670/8931881) and change it to read `$objectToArray: '$users'` and `{'array.v.name': 'user-name'}` – Greg Stanley Apr 05 '19 at 08:27

0 Answers0