Mongo db 查询和 [英] Mongo db query with and

查看:61
本文介绍了Mongo db 查询和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在mongo数据库中有我的数据,我的集合有两个字段,分别是created_attext,我想提取一个像bank这样的文档chilefintext 字段中,并且 created_at 值为 jan 15..我是 mongodb 的新手,当我尝试使用以下查询时,它给出的错误为意外令牌"

I have my data in mongo database, and my collection has two fields namely created_at, text, I want to extract a documents having the words like bank, chile and fin in the text field and having created_at value as jan 15.. I am new to mongodb and when I tried to use the below query it gives the error as "unexpected token"

查询:

db.tweet.find({$and : [{"created_at" : /.*jan 15.*/i}, {"text : /.*bank.*/i, /.*chile.*/i, /.*fin.*/i "}]})

请建议我更正.提前致谢

Please suggest me corrections.Thanks in advance

推荐答案

这是写错了.部分是重复使用 $and 不需要的地方,其次我认为你的意思是 $or 用于第二个条件.这实际上转化为使用最简单的正则表达式形式:

This is written wrong. Partly in a redundant use of $and where it is not needed, and secondly I think you mean $or for the second condition. Which actually translates to using the regex form in the easiest sense:

db.tweet.find({
    "created_at": /.*jan 15.*/i,
    "text": /bank|chile|fin/i
})

实际上,使用词边界"进行更精确的词"匹配:

Actually, use "word boundaries" for more exact "word" matching:

db.tweet.find({
    "created_at": /.*jan 15.*/i,
    "text": /\bbank\b|\bchile\b|\bfin\b/i
})

如果您实际上是指和",这意味着文本"字段必须包含所有"这些字符串,那么您需要一个 $and 运算符.但与你的做法不同:

If you do in fact mean "and" which means the "text" field must contain "all" of those strings, then you need an $and operator. But different to how you did it:

db.tweet.find({
    "created_at": /.*jan 15.*/i,
    "$and": [
        { "text": /.*bank.*/i },
        { "text": /.*chile.*/i },
        { "text": /.*fin.*/i }
    ]
}

$and 的目的 是允许数组"构造,其中针对不同条件引用相同"字段名称.这样结构才有效并且键名"不会重复.

The purpose of $and is to allow an "array" construct where the "same" field name is referenced for different conditions. This is so the structure is valid and "key names" are not duplicated.

否则所有 MongoDB 参数总是隐含的和"参数.

Otherwise all MongoDB arguments are implicitly an "and" argument always.

这篇关于Mongo db 查询和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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