Aris Μπ.
Aris Μπ.

Reputation: 67

MongoDB, array storage and Object querying

I am new to MongoDb and I've made a simple PHP filtering application (similar to 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.

The problem is When I try to remove "orange" from slug, the array breaks and turns into an Object

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

And I can no longer query it like this, as it returns NULL

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.

Unfortunately, I cannot change the indexes of the array because they are linked in other documents.

Any ideas? Thanks!

Upvotes: 2

Views: 258

Answers (1)

Abhishek
Abhishek

Reputation: 5728

You are getting null because you might be removing that document using remove method in php. Instead you need to update slug to array with only apple as its element

db.collection.update($array);

Upvotes: 1

Related Questions