Darkwing
Darkwing

Reputation: 7595

MongoDB remove returning nRemoved 0

I am running a remove command against the Mongo Shell which doesn't seem to remove any documents:

C:\Program Files\MongoDB\Server\3.0\bin>mongo outputcachedb --eval "db.CacheItem
s.remove({ "Expiration" : { "$lte" : new Date() } });"
2015-04-22T11:30:30.957+0200 I CONTROL  Hotfix KB2731284 or later update is not
installed, will zero-out data files
MongoDB shell version: 3.0.2
connecting to: outputcachedb
WriteResult({ "nRemoved" : 0 })

If I run the same query document using MongoVue it finds (and when requested removes) documents

Upvotes: 2

Views: 2988

Answers (2)

Dung
Dung

Reputation: 20585

That is because you have incorrect procedure to delete collection.

Solution and Example:

1- C:\MongoDB\Server\3.2\bin>mongo (do not issue command yet because you are not connected to any database yet, you are only connected to database server mongodb).

2-

show dbs analytics_database 0.000GB local 0.000GB test_database 0.000GB

3-

use test_database switched to db test_database

4-

db.Collection.remove({"_id": ObjectId("5694a3590f6d451c1500002e")}, 1); WriteResult({ "nRemoved" : 1 })

now you see WriteResult({ "nRemoved" : 1 }) is 1 not 0.

Done.

Upvotes: 3

badrequest400
badrequest400

Reputation: 180

I am not sure why --eval does not work, but I could get this to work by running an external .js file into the shell.

db.CacheItems.remove({ "Expiration" : { "$lte" : new Date() } });

goes into remove.js and then

$ mongo outputcachedb remove.js

Hope this works for you

Upvotes: 1

Related Questions