簡體   English   中英

使用非數組字段查找等於任何一個數組元素查詢的文檔

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

mongo db 模式變量

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

狀態存儲在數據庫中,如 0 或 1 或 2。用戶選擇的狀態搜索是數據數組,如

status: {1,2}

如何獲取包含任何一個數組元素的文檔。 我無法進行靜態搜索,因為數組大小每次都會改變

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

    console.log(body_status);

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

這對我有用。

我不知道我是否理解這個問題,但我認為你想要這樣的東西:

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

示例在這里

請檢查它是否按預期工作,在這種情況下,請使用更多信息更新問題。

或者,也許你想要的東西,像這樣

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM