gpt4 book ai didi

mongodb - 是否可以在数组键上查询 mongodb?

转载 作者:可可西里 更新时间:2023-11-01 09:56:42 26 4
gpt4 key购买 nike

我的表有一个由字符串索引的数组,我想要匹配这个字符串的所有记录,无论值是什么。例如获取 id1 为 fill 的所有记录:

var a = {
type: "Information",
ids: {
'id1' : '123'
'id2' : '456'
}
};


var b = {
type: "Information",
ids: {
'id1' : '789'
}
};

是否可以使用 mongodb 做到这一点?如何做到这一点?

最佳答案

您可以为此使用 $exists:

> db.things.insert({'type': 'Information', 'ids':{'id1': 123, 'id2': 456}})
> db.things.insert({'type': 'Information', 'ids':{'id1': 746, 'id2': 456}})
> db.things.insert({'type': 'Information', 'ids':{'id2': 456, 'id3': 936}})

> db.things.find({'ids.id1': {'$exists': true}})
{ "_id" : ObjectId("4dd3c706938307861ed610dd"), "type" : "Information", "ids" : { "id1" : 123, "id2" : 456 } }
{ "_id" : ObjectId("4dd3c7a1938307861ed610de"), "type" : "Information", "ids" : { "id1" : 746, "id2" : 456 } }

关于mongodb - 是否可以在数组键上查询 mongodb?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6045133/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com