This question already has answers here:
How to delete N numbers of documents in mongodb
(10 answers)
Closed 4 years ago.
I can remove newest 10 items in mysql using my next sql statement.
DELETE FROM `mytable` order by id desc limit 10
Is it possible to do the same in Mongodb using sort and limit?
I know how to find last 10 items
db.collection.find({}).sort("id", -1).limit(10)
but I'm not sure how delete items in one step.
You can do it in two steps like this:
const ids = db.collection.find({})
.sort("id", -1)
.limit(10)
.toArray()
.map(ele => ele._id);
db.collection.remove({_id: {$in: ids}})
Related
This question already has answers here:
How to limit number of updating documents in mongodb
(8 answers)
Closed 5 years ago.
Lets say I have a 10 documents of Item in the database.
Lets retrieve 3 documents of Item matching some condition using limit().
documents = Item.objects(somefield=somecondition).limit(3)
Now if I do
documents.update(), mongoengine updates all the documents in the database matched by the query not just the 3 documents I have limited my query to.
I also tried setting multi=False in the params, but then only one document gets updated.
Is there anyway to do update while querying itself instead of looping over the documents one by one?
As far as I know there is no available solution to your problem provided by MongoDB. However you could try something like this
documents.forEach(
function (e) {
e.field = 'value';
db.collection.save(e);
}
);
This question already has answers here:
Does MongoDB's $in clause guarantee order
(11 answers)
Closed 8 years ago.
I want to order the records in result set. How can I receive the results in MongoDB like what I expected. In MySQL, I can use the following query to get my expected way of sort.
But how can I get in MongoDB.
SELECT * FROM fruit
ORDER BY FIELD(name, 'Banana', 'Apple', 'Pear', 'Orange'), variety;
Please advice. Thanks!
I guess this will help:
db.users.find({},{_id:0,"accounts":1,"name":1});
But i have never tried ordering fields.
This question already has answers here:
What is the maximum number of parameters passed to $in query in MongoDB?
(4 answers)
Closed 6 years ago.
Was just wondering if there is a limit to Mongodb's $in function?
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24in
I have a collection of users (BIG) and have a smaller subset of ObjectIds stashed somewhere, and I want to select all users (collections) that are in my ObjectIds.
Thanks
Since there's no limit on the number of items in an array as such, you shouldn't have any problem..
For the case when the array is embedded inside a document, you might want to have a look at these:
http://groups.google.com/group/mongodb-user/browse_thread/thread/4a7caeba972aa998?fwc=1
Filtering content based on words
http://groups.google.com/group/mongodb-user/browse_thread/thread/28ae76e5ad5fcfb5
This question already has answers here:
How to get the last N records in mongodb?
(16 answers)
Closed 4 years ago.
How can I get last 50 documents in mongoDB?
I have a collection which is made by
db.createCollection("collection",{capped:true, size:300000});
from this "collection"
I would like to have last 50 documents instead of get first 50 documents.
I know that I can get first 50 documents by using
db.collection.find().limit(50);
But how can I get last 50 documents?
Is this can be done simply with MongoDB API or should I implement this with programming?
this should do the thing:
db.collection.find().sort({$natural: -1}).limit(50);
The last N added records, from less recent to most recent, can be seen with this query:
db.collection.find().skip(db.collection.count() - N)
In your case 50
db.collection.find().skip(db.collection.count() - 50)
you need the last 50 documents from mongo collection, so this is the query
db.collection('collectionName').find().sort({$natural: -1}).limit(50);
// sort({$natural: -1}) for the last fields (desecnding)
// limit(50) because you need only 50 fields
This question already has answers here:
A list of indices in MongoDB?
(7 answers)
Closed 8 years ago.
I just want to display all the indexes in the shell.
If you want raw access to the indexes, you can query the db.system.indexes collection:
> db.system.indexes.find()
To find the indexes for a specific collection, you can do:
> db.collection.getIndexes()
See this question.