Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deleteMany not working with $in filter #100

Closed
acim opened this issue Jan 20, 2016 · 9 comments
Closed

deleteMany not working with $in filter #100

acim opened this issue Jan 20, 2016 · 9 comments

Comments

@acim
Copy link

acim commented Jan 20, 2016

Am I doing something wrong here or this is a bug?

/** @var Collection $collection */
$collection->deleteMany(['_id' => ['$in', [1, 2, 3, 4, 5]]]);

So, I can see documents with _id's 1, 2, 3, 4, 5 in my MongoDB, but they are not deleted by this command.

@jmikola
Copy link
Member

jmikola commented Jan 20, 2016

Try replacing the comma after '$in' with a =>.

$collection->deleteMany(['_id' => ['$in' => [1, 2, 3, 4, 5]]]);

@acim
Copy link
Author

acim commented Jan 21, 2016

Sorry, I wrote it like you say actually, comma above is just a typo. So, this is still not working :(

$collection->deleteMany(['_id' => ['$in' => [1, 2, 3, 4, 5]]]);

@jmikola
Copy link
Member

jmikola commented Jan 25, 2016

I can't reproduce this with the following script:

<?php

require 'vendor/autoload.php';

$collection = (new MongoDB\Client)->test->foo;
$collection->drop();

$result = $collection->insertMany([
    ['_id' => 1],
    ['_id' => 2],
    ['_id' => 3],
    ['_id' => 4],
    ['_id' => 5],
]);

printf("Inserted %d documents\n", $result->getInsertedCount());
echo "Dumping documents in collection:\n";

foreach ($collection->find() as $document) {
    echo MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($document)), "\n";
}

$result = $collection->deleteMany(['_id' => ['$in' => [1, 2, 4, 5]]]);

printf("Deleted %d documents\n", $result->getDeletedCount());
echo "Dumping documents in collection:\n";

foreach ($collection->find() as $document) {
    echo MongoDB\BSON\toJSON(MongoDB\BSON\fromPHP($document)), "\n";
}

Output for me:

$ php foo.php 
Inserted 5 documents
Dumping documents in collection:
{ "_id" : 1 }
{ "_id" : 2 }
{ "_id" : 3 }
{ "_id" : 4 }
{ "_id" : 5 }
Deleted 4 documents
Dumping documents in collection:
{ "_id" : 3 }

Have you tried running the same query in the shell and/or inspecting the types of the _id fields? For querying purposes, the server should consider like numeric values of different types equivalent, so I don't believe there would be any issue if IDs were a mixture of floats and 32- or 64-bit integers; however, it wouldn't hurt to inspect the types in the shell or with bsondump (and its debug output format).

@liufee
Copy link

liufee commented Jan 26, 2016

please help me how to delete a record by _id?i do it just like $collection->->deleteOne(['_id'=>'56a5ec134242b50338006ee2']),but it doesn't work.

@acim
Copy link
Author

acim commented Jan 26, 2016

@jmikola Yes, this is working. It seems I didn't have some documents within the collection before deletion. I actually this is working as a query, but obviously not. So I have to do find before deleteMany. Thank you very much.

@acim acim closed this as completed Jan 26, 2016
@jmikola
Copy link
Member

jmikola commented Feb 12, 2016

please help me how to delete a record by _id?i do it just like $collection->->deleteOne(['_id'=>'56a5ec134242b50338006ee2']),but it doesn't work.

@liufee: I assume "56a5ec134242b50338006ee2" in your case corresponds to an ObjectID. In that case, you should be using the MongoDB\BSON\ObjectID class in your criteria, which you can construct from a 24-character, hexadecimal string. The query you were running is attempting to match _id against a string type and value, which it is not.

In the future, please open a new issue instead of replying onto an existing issue, especially since your question was not a duplicate of the OP's issue.

@liufee
Copy link

liufee commented Feb 18, 2016

got it thank you

@rathipradha
Copy link

rathipradha commented May 18, 2020

deletemany is not function (ERROR)

rest.deleteMany({'_id': {$in:[ObjectId("5ae4c2aa3bf923ae6cda6a67"),ObjectId("5b068e30b901e0547455fe60"),ObjectId("5b0bd3431f135383c812d0e5")]} }), function (err,doc) {

})

mongod --->db version v4.2.3

@jmikola
Copy link
Member

jmikola commented May 18, 2020

The above comment is a duplicate of #745.

@mongodb mongodb locked as resolved and limited conversation to collaborators May 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants