0

I have a dataset like this:

{
 page1: ["login", "registration"], // Here multiple pagename will store
 page2: ["5c35f1045643180d9488221f"] // Here Multiple id will store
},
{
 page1: ["login", "registration"],
 page2: []
}

So on my data query I want to get data in which page1 === "login" and page2 === "5c35f1045643180d9488112f".

So I tried to get this through $and query but I am not getting anything.

My code is this:

db.collection.find(
 {
  $and: [
    {page1: {$in: ["login"]}},
    {page2: {$in: ["5c35f1045643180d9488221f"]}}
  ]
 }
)

I don't know where I am doing mistake. Kindly suggest me if I am wrong or wrote the wrong statement on query. Thanks in advance.

Aks
  • 1,092
  • 3
  • 12
  • 29
  • Does this answer your question ? https://stackoverflow.com/questions/6578178/node-js-mongoose-js-string-to-objectid-function . You need to check type of inputs does match with type of field values in DB or not, If your `page2` field is of type `ObjectId()` in DB then you need to convert input from string to `ObjectId()` in code & hit the query.. – whoami - fakeFaceTrueSoul May 15 '20 at 15:14

2 Answers2

0

[edited] Try

db.collection.find({page1: "login", page2: "5c35f1045643180d9488221f"})

if this does not work, the value is wrong. check the value of page 2 again

adhi narayan
  • 310
  • 2
  • 11
0

If page2 is an array of ObjectIds not strings, then you may use something like that

db.collection.find({
  page1: "login",
  page2: ObjectId("5c35f1045643180d9488221f")
})

you can test it here Mongo Playground

hope it helps

Mohammed Yousry
  • 2,134
  • 1
  • 5
  • 7