MongoDB 文本索引数组对象列 [英] MongoDB text indexing array objects columns

查看:47
本文介绍了MongoDB 文本索引数组对象列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我试图对一个文档列表进行文本索引:

I have a problem that I am trying to text index a document that is a list of documents:

我首先插入数据库:

db.userlikes.insert({
   "data": [
    {
       "category": "Non-profit organization",
       "name": "Animals"
    },
    {
       "category": "Community",
       "name": "Computers"
    }
   ]
})

然后我尝试为类别列创建索引:

Then I am trying to create the index for the category column:

db.userlikes.ensureIndex({"data.category": "text"})

但是当我尝试搜索时,我没有得到任何结果:

But when I am trying to search I get no results:

db.userlikes.find({"data.category": "profit"})

不能以这种方式建立索引吗?最好的方法是将每个文档分别插入数组(数据")中吗?这将是一种开销,因为我只想插入我从 API 获得的数据库响应.

请帮帮我.

谢谢

推荐答案

我尝试了与 EA9 相同的步骤.它似乎对我有用.您可能想在某处检查任何拼写错误.这是我的命令输出.

I tried the same steps EA9. And it seems to work for me. You might want to check for any spelling mistakes somewhere. Here is my output with the commands.

db.userlikes.insert({
...    "data": [
...     {
...        "category": "Non-profit organization",
...        "name": "Animals"
...     },
...     {
...        "category": "Community",
...        "name": "Computers"
...     }
...    ]
... });
WriteResult({ "nInserted" : 1 })

db.userlikes.find();

{ "_id" : ObjectId("541ff8dc1dca72499395cbcf"), "data" : [ { "category" : "Non-profit organization", "name" : "Animals" }, { "category" : "Community", "name" : "Computers" } ] }


db.userlikes.ensureIndex({"data.category": "text"})


{
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 1,
    "numIndexesAfter" : 2,
    "ok" : 1
}


 db.userlikes.find({ $text: { $search: "profit" } });


{ "_id" : ObjectId("541ff8dc1dca72499395cbcf"), "data" : [ { "category" : "Non-profit organization", "name" : "Animals" }, { "category" : "Community", "name" : "Computers" } ] }

这篇关于MongoDB 文本索引数组对象列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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