-
Notifications
You must be signed in to change notification settings - Fork 260
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
Comments
Try replacing the comma after
|
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]]]); |
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:
Have you tried running the same query in the shell and/or inspecting the types of the |
please help me how to delete a record by _id?i do it just like $collection->->deleteOne(['_id'=>'56a5ec134242b50338006ee2']),but it doesn't work. |
@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. |
@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 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. |
got it thank you |
deletemany is not function (ERROR) rest.deleteMany({'_id': {$in:[ObjectId("5ae4c2aa3bf923ae6cda6a67"),ObjectId("5b068e30b901e0547455fe60"),ObjectId("5b0bd3431f135383c812d0e5")]} }), function (err,doc) { }) mongod --->db version v4.2.3 |
The above comment is a duplicate of #745. |
Am I doing something wrong here or this is a bug?
So, I can see documents with _id's 1, 2, 3, 4, 5 in my MongoDB, but they are not deleted by this command.
The text was updated successfully, but these errors were encountered: