简体   繁体   English

MongoDB,数组存储和对象查询

[英]MongoDB, array storage and Object querying

I am new to MongoDb and I've made a simple PHP filtering application (similar to TAGS) 我是MongoDb的新手,并且制作了一个简单的PHP过滤应用程序(类似于TAGS)

I Have a collection which is called "Filters" 我有一个名为“过滤器”的集合

Inside of it, each record looks like this: 在其中,每条记录如下所示:

    "_id" : ObjectId("5274da3e040b61fa15000001"),
"activation" : 1,
"and_or" : "",
"description" : "",
"id" : 13,
"order_id" : 2,
"slug" : [
    "apple",
    "orange",
    "banana"
],
"title" : "Sample"

I take those values from a PHP form and insert them like a record. 我从PHP表单中获取这些值,然后像记录一样插入它们。

The problem is When I try to remove "orange" from slug , the array breaks and turns into an Object 问题是当我尝试从slug中删除“ orange”时,数组中断并变成一个Object

"slug" : {
    "0" : "apple",
    "2" : "banana"
},

And I can no longer query it like this, as it returns NULL 而且我无法再像这样查询它,因为它返回NULL

db.filters.find({slug: "apple"}) db.filters.find({slug:“ apple”})

null

I assume that when an array's index is numerically correct, (1, 2, 3, 4) then Mongo will recognize it as an array, otherwise it stores it as an object as custom index is used. 我假设当数组的索引在数字上正确(1、2、3、4)时,Mongo会将其识别为数组,否则它将作为自定义索引使用时存储为对象。

Unfortunately, I cannot change the indexes of the array because they are linked in other documents. 不幸的是,我无法更改数组的索引,因为它们在其他文档中链接。

Any ideas? 有任何想法吗? Thanks! 谢谢!

You are getting null because you might be removing that document using remove method in php. 您得到的是空值,因为您可能正在使用php中的remove方法删除该文档。 Instead you need to update slug to array with only apple as its element 相反,您需要将子弹更新为仅以Apple为元素的数组

db.collection.update($array); db.collection.update($阵列);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM