【问题标题】:find elements in array available with mongodb [duplicate]在mongodb可用的数组中查找元素[重复]
【发布时间】:2020-03-26 16:49:47
【问题描述】:

exp我的收藏用户

[
    {
        _id: ObjectId('123456')
        username: 'test'
    },
    {
        _id: ObjectId('654321')
        username: 'test2'
    },
    {
        _id: ObjectId('789101')
        username: 'test3'
    }
]

我的数组可用:

var usersId = [ObjectId('123456'), ObjectId('654321')]

我想使用字段用户名查找并且仅在数组 usersId 中可用

我的测试不工作:

db.users.find({ 
  username: keyword,
  _id: { $in: usersId  }
})

【问题讨论】:

标签: javascript node.js mongodb mongoose


【解决方案1】:

你可以试试下面的代码。

db.users.find({ 
  username: keyword,
  _id: { $in: [usersId] }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 2017-08-18
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多