问题描述
{
person-name是一个集合,其中的文档如下所示: :Hughart,Ron,
info:{
birthnotes:[美国加利福尼亚州洛杉矶],
出生日期:[1961年6月18日] ,
birthname:[Hughart,Ronald P]
}
}
我想找到在里斯本出生的人。问题是如何知道记录的字段info.birthnotes
是否包含Lisbon
?
我试过这个命令:
db.collection.find({info .birthnotes:{$ in:[Lisbon]}})
什么都没有。
从检查中,info.birthnotes
数组可能看起来像逗号分隔的元素
info.birthnotes:[Los Angeles,California,USA ]
但它有一个字符串值Los Angeles,California,美国
这是逗号分隔:
info.birthnotes:[Los Angeles,California ,美国]
您目前正在查询它,就好像它是具有单个sttring值的多值元素。您需要使用 基于查询来返回其info.birthnotes
数组字符串包含Lisbon
如下:
db.collection.find({info.birthnotes:{$正则表达式:/里斯本/ i}})
或者如果您使用 构造函数创建一个正则表达式对象,您可以在您的查询中使用:
$ b $ pre $ var $ query $里斯本;
var rgx = new RegExp(query,i);
db.collection.find({info.birthnotes:rgx})
I have a collection, the document in it looks like this:
{
"person-name" : "Hughart, Ron",
"info" : {
"birthnotes" : [ "Los Angeles, California, USA" ],
"birthdate" : [ "18 June 1961" ],
"birthname" : [ "Hughart, Ronald P" ]
}
}
I want to find the people who were born in Lisbon. The question is how do I know if a record's field "info.birthnotes"
contains "Lisbon"
?
I tried this command:
db.collection.find({"info.birthnotes": {"$in": ["Lisbon"]}})
but it returns nothing.
From inspection, the "info.birthnotes"
array may look like it has comma separated elements
"info.birthnotes" : [ "Los Angeles", "California", "USA" ]
yet it has a single string value "Los Angeles, California, USA"
which is comma separated:
"info.birthnotes" : [ "Los Angeles, California, USA" ]
You are currently querying it as if it is multi-valued with single sttring values as elements. You need to use a $regex
based query to return the documents whose "info.birthnotes"
array string contains "Lisbon"
as follows:
db.collection.find({ "info.birthnotes": { "$regex": /Lisbon/i } })
or if you are using a variable with the RegExp
constructor to create a regular expression object you can use in your query:
var query = "Lisbon";
var rgx = new RegExp(query, "i");
db.collection.find({"info.birthnotes": rgx})
这篇关于查找其对象包含String值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!