mongodb:如何在文本索引中搜索整数?
我正在尝试为mongo集合创建一个文本索引,以便能够进行文本搜索 虽然文本搜索非常适合使用文本搜索索引,但我无法搜索我在索引中添加的整数字段 我正在使用mongomongodb:如何在文本索引中搜索整数?,mongodb,Mongodb,我正在尝试为mongo集合创建一个文本索引,以便能够进行文本搜索 虽然文本搜索非常适合使用文本搜索索引,但我无法搜索我在索引中添加的整数字段 我正在使用mongov3.0.7 以下是复制问题的步骤: > <!-- added two dummy fields -->; > db.test.insert({a: 1, b: "apple"}); WriteResult({ "nInserted" : 1 }) > > db.test.insert({a: 2})
v3.0.7
以下是复制问题的步骤:
> <!-- added two dummy fields -->;
> db.test.insert({a: 1, b: "apple"});
WriteResult({ "nInserted" : 1 })
>
> db.test.insert({a: 2});
WriteResult({ "nInserted" : 1 })
>
>
> <!-- initially only default index exists -->;
> db.test.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "3dphy_dev.test"
}
]
>
>
> <!-- created new text index -->;
> db.test.createIndex({"a": "text", "b": "text"});
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
>
>
> db.test.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "3dphy_dev.test"
},
{
"v" : 1,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "a_text_b_text",
"ns" : "3dphy_dev.test",
"weights" : {
"a" : 1,
"b" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 2
}
]
>
>
> <!-- searching for text yields result -->;
> db.test.find({$text: {$search: "apple"}});
{ "_id" : ObjectId("56497f6f96621a852197891b"), "a" : 1, "b" : "apple"}
>
>
> <!-- integer search doesn't works -->;
> db.test.find({$text: {$search: "1"}});
> db.test.find({$text: {$search: "2"}});
>;
>insert({a:1,b:apple});
WriteResult({“n插入”:1})
>
>insert({a:2});
WriteResult({“n插入”:1})
>
>
> ;
>db.test.getIndexes();
[
{
“v”:1,
“关键”:{
“_id”:1
},
“姓名”:“身份证”,
“ns”:“三维物理开发测试”
}
]
>
>
> ;
>createIndex({“a”:“text”,“b”:“text”});
{
“createdCollectionAutomatically”:false,
“numIndexesBefore”:1,
“numIndexesAfter”:2,
“好”:1
}
>
>
>db.test.getIndexes();
[
{
“v”:1,
“关键”:{
“_id”:1
},
“姓名”:“身份证”,
“ns”:“三维物理开发测试”
},
{
“v”:1,
“关键”:{
“_fts”:“文本”,
“_ftsx”:1
},
“名称”:“a_text_b_text”,
“ns”:“3dphy\u dev.test”,
“重量”:{
“a”:1,
“b”:1
},
“默认语言”:“英语”,
“语言覆盖”:“语言”,
“textIndexVersion”:2
}
]
>
>
> ;
>find({$text:{$search:{$apple}});
{“_id”:ObjectId(“56497f6f96621a852197891b”),“a”:1,“b”:“苹果”}
>
>
> ;
>find({$text:{$search:“1”}});
>find({$text:{$search:“2”}});
最后两个搜索命令不产生任何结果
我正在尝试为我的搜索构建一个api端点,在这里我可以搜索集合中的文本和整数数据
Plz建议 第二次搜索不起作用,因为
$search
的类型不是字符串(“1”
或“2”
)。你试过这个吗:
db.test.find({a:1});
第二次搜索不起作用,因为$search
的类型不是String
(“1”
或“2”
)。你试过这个吗:
db.test.find({a:1});
是的,我知道你的建议行得通。但就像我说的,我想为文本和数字搜索创建一个端点,而不必担心查询字符串实际存储在哪个字段中。或者区分文本和数字查询字符串是的,我知道你的建议会奏效。但就像我说的,我想为文本和数字搜索创建一个端点,而不必担心查询字符串实际存储在哪个字段中。或者区分文本和数字查询字符串