Santhosh
Santhosh

Reputation: 47

find the document which equals any one of the array element query with a non array field

mongo db schema variable

 status:{
    type: Number,
    enum: [0,1,2,3,4,5], //0-NOT ACCEPTED,1-COMPLETED,2-PENDING
    default: 0
  }

status stored in db like 0 or 1 or 2. status search with user selection is array of datas like

status: {1,2}

how to get the documents which has any one the of the array element. I can't do a static search because array size can change every time

  // if(status){
    //         query = {
    //           ...query,
    //           "status": status
    //         }
    // }

    console.log(body_status);

    if(body_status){
      query = {
        ...query,
        "status": {"$in":body_status}
      }
}

this works for me.

Upvotes: 0

Views: 33

Answers (1)

J.F.
J.F.

Reputation: 15235

I don't know if I've understand the question but I think you want something like this:

db.collection.find({
  "status": {
    "$in": [
      1,
      2,
      4
    ]
  }
})

Example here

Please check if it works as expected or not and in this case update the question with more information.

Or maybe you want something like this:

db.collection.find({
  "status": 1
})

Upvotes: 1

Related Questions