How to delete N numbers of documents in mongodb
In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query
db.collectionsname.find().sort({"timestamp"-1}).limit(10)
This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query
db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})
but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?
mongodb
add a comment |
In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query
db.collectionsname.find().sort({"timestamp"-1}).limit(10)
This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query
db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})
but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?
mongodb
1
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03
add a comment |
In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query
db.collectionsname.find().sort({"timestamp"-1}).limit(10)
This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query
db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})
but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?
mongodb
In my collections, documents contains key like status and timestamp. When I want to find latest ten documents then I write following query
db.collectionsname.find().sort({"timestamp"-1}).limit(10)
This query gives me results which I want but when I want to delete latest ten documents then I was writing the following query
db.collectionsname.remove({"status":0},10).sort({"timestamp":-1})
but it shows following error
TypeError: Cannot call method 'sort' of undefined
and again I wrote the same query as below
db.collectionsname.remove({"status":0},10)
It deletes only one document. So how can I write a query which deletes ten latest documents and sorts on timestamp?
mongodb
mongodb
edited Aug 31 '16 at 14:02


Shabbir Essaji
111110
111110
asked Sep 28 '13 at 9:53
YogeshYogesh
5,63432144
5,63432144
1
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03
add a comment |
1
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03
1
1
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03
add a comment |
4 Answers
4
active
oldest
votes
You can't set a limit when using remove
or findAndModify
. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids
Then pass the returned _id
s to the remove method:
db.collectionName.remove({_id: {$in: removeIdsArray}})
FYI: you cannot remove documents from a capped collection.
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
Correct me if I'm wrong, but it makes difference whetherlimit(100)
comes beforesort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write.limit(100).sort({timestamp:-1})
, it would make a difference.
– ULazdins
Oct 14 '15 at 14:46
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
add a comment |
Another way is to write a python script.
from pymongo import MongoClient
def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000
for document in cursor:
id = document.get("_id")
if total_number_of_records == 100:
break
delete_query = {"_id": id}
collection.delete_one(delete_query)
total_number_of_records -= 1
if __name__ == "__main__":
# execute only if run as a script
main()
add a comment |
Let N be number of records to delete.
db.collectionName.find().limit(N).forEach(doc =>
{
db.collectionName.remove({_id:doc._id})
}
)
add a comment |
Below query will find and delete the latest 10 documents from collection:-
db.collectionsname.findAndModify({
query: { 'status':0 },
sort: { 'timestamp': -1 },
limit: 10,
remove: true
});
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19065615%2fhow-to-delete-n-numbers-of-documents-in-mongodb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't set a limit when using remove
or findAndModify
. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids
Then pass the returned _id
s to the remove method:
db.collectionName.remove({_id: {$in: removeIdsArray}})
FYI: you cannot remove documents from a capped collection.
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
Correct me if I'm wrong, but it makes difference whetherlimit(100)
comes beforesort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write.limit(100).sort({timestamp:-1})
, it would make a difference.
– ULazdins
Oct 14 '15 at 14:46
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
add a comment |
You can't set a limit when using remove
or findAndModify
. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids
Then pass the returned _id
s to the remove method:
db.collectionName.remove({_id: {$in: removeIdsArray}})
FYI: you cannot remove documents from a capped collection.
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
Correct me if I'm wrong, but it makes difference whetherlimit(100)
comes beforesort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write.limit(100).sort({timestamp:-1})
, it would make a difference.
– ULazdins
Oct 14 '15 at 14:46
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
add a comment |
You can't set a limit when using remove
or findAndModify
. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids
Then pass the returned _id
s to the remove method:
db.collectionName.remove({_id: {$in: removeIdsArray}})
FYI: you cannot remove documents from a capped collection.
You can't set a limit when using remove
or findAndModify
. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.collectionName.find({}, {_id : 1})
.limit(100)
.sort({timestamp:-1})
.toArray()
.map(function(doc) { return doc._id; }); // Pull out just the _ids
Then pass the returned _id
s to the remove method:
db.collectionName.remove({_id: {$in: removeIdsArray}})
FYI: you cannot remove documents from a capped collection.
edited May 31 '17 at 14:30


Nuri Tasdemir
7,93512545
7,93512545
answered Sep 28 '13 at 11:15
WiredPrairieWiredPrairie
46.1k1384121
46.1k1384121
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
Correct me if I'm wrong, but it makes difference whetherlimit(100)
comes beforesort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write.limit(100).sort({timestamp:-1})
, it would make a difference.
– ULazdins
Oct 14 '15 at 14:46
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
add a comment |
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
Correct me if I'm wrong, but it makes difference whetherlimit(100)
comes beforesort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write.limit(100).sort({timestamp:-1})
, it would make a difference.
– ULazdins
Oct 14 '15 at 14:46
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
Who do you have to do the sort part if all the records are going to be deleted?
– ULazdins
Jul 22 '14 at 12:52
2
2
Correct me if I'm wrong, but it makes difference whether
limit(100)
comes before sort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1})
, it would make a difference.– ULazdins
Oct 14 '15 at 14:46
Correct me if I'm wrong, but it makes difference whether
limit(100)
comes before sort({timestamp:-1})
or after. In the example @WiredPrairie limits results to 100 and sorts them afterwards (.sort({timestamp:-1}).limit(100)
). In this case the sorting statement has no effect on the records returned. On the other hand, if you would write .limit(100).sort({timestamp:-1})
, it would make a difference.– ULazdins
Oct 14 '15 at 14:46
10
10
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@ULazdins "Mongo applies the sort before limiting the results regardless of the order you call sort and limit on the cursor." (source)
– joeytwiddle
Apr 5 '16 at 3:37
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
@Madbreaks Afforementioned "new" answer is wrong. Read the comment under that.
– Nuri Tasdemir
May 31 '17 at 14:31
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
what if the numbers is large? I got the error : "Converting from JavaScript to BSON failed: Object size 19888938 exceeds limit of 16793600 bytes. "
– Jaskey
Sep 13 '17 at 6:14
add a comment |
Another way is to write a python script.
from pymongo import MongoClient
def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000
for document in cursor:
id = document.get("_id")
if total_number_of_records == 100:
break
delete_query = {"_id": id}
collection.delete_one(delete_query)
total_number_of_records -= 1
if __name__ == "__main__":
# execute only if run as a script
main()
add a comment |
Another way is to write a python script.
from pymongo import MongoClient
def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000
for document in cursor:
id = document.get("_id")
if total_number_of_records == 100:
break
delete_query = {"_id": id}
collection.delete_one(delete_query)
total_number_of_records -= 1
if __name__ == "__main__":
# execute only if run as a script
main()
add a comment |
Another way is to write a python script.
from pymongo import MongoClient
def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000
for document in cursor:
id = document.get("_id")
if total_number_of_records == 100:
break
delete_query = {"_id": id}
collection.delete_one(delete_query)
total_number_of_records -= 1
if __name__ == "__main__":
# execute only if run as a script
main()
Another way is to write a python script.
from pymongo import MongoClient
def main():
local_client = MongoClient()
collection = local_client.database.collection
cursor = collection.find()
total_number_of_records = 10000
for document in cursor:
id = document.get("_id")
if total_number_of_records == 100:
break
delete_query = {"_id": id}
collection.delete_one(delete_query)
total_number_of_records -= 1
if __name__ == "__main__":
# execute only if run as a script
main()
edited Nov 20 '18 at 19:49
answered Nov 20 '18 at 18:40
jellyDeanjellyDean
5010
5010
add a comment |
add a comment |
Let N be number of records to delete.
db.collectionName.find().limit(N).forEach(doc =>
{
db.collectionName.remove({_id:doc._id})
}
)
add a comment |
Let N be number of records to delete.
db.collectionName.find().limit(N).forEach(doc =>
{
db.collectionName.remove({_id:doc._id})
}
)
add a comment |
Let N be number of records to delete.
db.collectionName.find().limit(N).forEach(doc =>
{
db.collectionName.remove({_id:doc._id})
}
)
Let N be number of records to delete.
db.collectionName.find().limit(N).forEach(doc =>
{
db.collectionName.remove({_id:doc._id})
}
)
answered Dec 27 '18 at 12:32
Prashant SharmaPrashant Sharma
616
616
add a comment |
add a comment |
Below query will find and delete the latest 10 documents from collection:-
db.collectionsname.findAndModify({
query: { 'status':0 },
sort: { 'timestamp': -1 },
limit: 10,
remove: true
});
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
add a comment |
Below query will find and delete the latest 10 documents from collection:-
db.collectionsname.findAndModify({
query: { 'status':0 },
sort: { 'timestamp': -1 },
limit: 10,
remove: true
});
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
add a comment |
Below query will find and delete the latest 10 documents from collection:-
db.collectionsname.findAndModify({
query: { 'status':0 },
sort: { 'timestamp': -1 },
limit: 10,
remove: true
});
Below query will find and delete the latest 10 documents from collection:-
db.collectionsname.findAndModify({
query: { 'status':0 },
sort: { 'timestamp': -1 },
limit: 10,
remove: true
});
edited Jul 6 '17 at 13:50
Sergio Tulentsev
180k30289305
180k30289305
answered Apr 12 '17 at 5:31
anonymusanonymus
253
253
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
add a comment |
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
5
5
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
docs.mongodb.com/manual/reference/method/… This doesn't support a "limit" field, and in fact if you try it, you will find it only deletes 1 element (as that is the default logic)
– tweak2
May 15 '17 at 16:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f19065615%2fhow-to-delete-n-numbers-of-documents-in-mongodb%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
That might help you docs.mongodb.org/manual/reference/command/findAndModify
– Mina
Sep 28 '13 at 10:03