nope
nope

Reputation: 1078

Using php and mongodb, how do you remove a document by type

I have read the documentation and searched all over but I can't find how to do it. If I have a document inside a collection and the document is an empty array I can remove it like this:

db.data.remove(array('document'=>array()))

or like this:

db.data.remove(array('document'=>[]))

But if the array is not empty it doesn't work, so I tried like this:

db.data.remove(array('document'=>array('$type'=>4)))

Am I doing it wrong? Is there any way to do this? Removing the document on another criteria isn't useful for me.

Upvotes: 1

Views: 118

Answers (1)

Julian Hollmann
Julian Hollmann

Reputation: 2912

Edit: Finally I found out what your problem is.

// test document
{ "_id" : ObjectId("4fe1af4dd404b1863ff20aac"), "document" : [ 1 ] }

The $type operator looks inside the array, not the array itself so the following find returns nothing:

db.test.find({document : { $type : 4  } })

Now in my testcase the document contains a double, the following find returns the document:

db.test.find({document : { $type : 1  } })
{ "_id" : ObjectId("4fe1af4dd404b1863ff20aac"), "document" : [ 1 ] }

Please see also the bug report about this topic: https://jira.mongodb.org/browse/SERVER-1475

Upvotes: 2

Related Questions