Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String Mongodb查询字段值(如果包含在长字符串中)_String_Mongodb_Include_Find - Fatal编程技术网

String Mongodb查询字段值(如果包含在长字符串中)

String Mongodb查询字段值(如果包含在长字符串中),string,mongodb,include,find,String,Mongodb,Include,Find,给定stringX=“term | quote | text”。我想在mongodb上查询以下文档: [{ id:"1", description:"term" }, { id:"2", description:"term2" }, { id:"3", description:"term3" }] 如何找到id为1的文档?伪查询类似于: "find document, which description is included (to be found) in stringX".

给定
stringX=“term | quote | text”
。我想在mongodb上查询以下文档:

[{
 id:"1",
 description:"term"
},
{
 id:"2",
 description:"term2"
},
{
 id:"3",
 description:"term3"
}]
如何找到id为1的文档?伪查询类似于:

"find document, which description is included (to be found) in stringX".

使用字符串中的运算符创建查询表达式。您的最终查询对象应类似于:

var query = {
    "description": { "$in": ["term", "quote", "text"] }
}
或者,如果您喜欢使用:

然后,您可以在查询asin中使用它

db.collection.find(query)
要以第一种形式获取查询,使用管道作为分隔符的输入字符串,并使用结果数组作为运算符的值:

var query = {
    "description": { "$in": stringX.split("|") }
};
db.collection.find(query)

对于第二种形式,考虑在String字符串上使用<强> < /强>方法,如下面的

var orOperator = stringX.split("|").map(function (str){ return { "description": str } }),
    query = { "$or": orOperator };
db.collection.find(query)

唯一的解决办法是每个搜索词都有一个
$或
。谢谢chridam!我尝试了解决方案1,它成功了!很好的解决方案。
var orOperator = stringX.split("|").map(function (str){ return { "description": str } }),
    query = { "$or": orOperator };
db.collection.find(query)