【问题标题】:Searching MongoDB arrays without respect for order在不考虑顺序的情况下搜索 MongoDB 数组
【发布时间】:2013-08-22 19:40:17
【问题描述】:

鉴于 MongoDB 中条目的以下结构:

{
_id: blahblah,
string_list: ['hello','large','world']
}

有没有办法执行如下搜索:

db.test.find({string_list:['large','world','hello']})

这将返回在string_list 字段中具有'hello''large''world' 的任何文档(但没有其他字符串,只有这三个)以任何顺序?

我知道{$all:...} 'operator' 会做类似的事情,但据我所知,如果我使用过:

db.collection.find({string_list: {$all:['large','world','hello']}})

它还会返回以下条目:

{
_id: blahblah,
string_list: ['hello','large','world','two']
}

我不想要的。

【问题讨论】:

    标签: arrays mongodb search database


    【解决方案1】:

    $all$size 运算符一起使用,即:

    db.collection.find({"string_list" : {$all:["large","world","hello"]}, "string_list": {$size: 3}});
    

    【讨论】:

    • 完美解决方案。谢谢。
    • @CharlesSpinosa 欢迎来到 StackOverflow!如果这个答案解决了您的问题,请点击投票箭头旁边的复选框接受它。
    • @ChrisHeald 哎呀!感谢您的提醒。接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-28
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多