查找其对象包含String值的记录 [英] Finding records whose object contains a String value

查看:288
本文介绍了查找其对象包含String值的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  {
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值的多值元素。您需要使用 $ regex code> 基于查询来返回其info.birthnotes数组字符串包含Lisbon 如下:

  db.collection.find({info.birthnotes:{$正则表达式:/里斯本/ i}})

或者如果您使用 RegExp 构造函数创建一个正则表达式对象,您可以在您的查询中使用:
$ 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值的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆