2

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!

4
  • Or maybe there is a away of actually searching inside and Object in MongoDB and returning the key? Commented Nov 21, 2013 at 10:55
  • stackoverflow.com/questions/4058748/… might help Commented Nov 21, 2013 at 11:48
  • How exactly do you "remove orange from slug"? Commented Nov 21, 2013 at 15:45
  • by submitting the form with 2 inputs form[slug][0] = apple and form[slug][2] = Banana... without problem in php Commented Nov 21, 2013 at 20:28

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.