简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM