This question already has answers here:
Does mongodb automatically create an index on the _id field of embedded documents?
(3 answers)
Closed 1 year ago.
I have nested array of objects with _id inside. I know, that collection has default, top level, index created on _id column. Mu question is - should i create index on nested _id columns or mongodb has a convention that all _id named columns will be indexed?
Thanks
if you want you should create an index for nested _id
mongo by default index _id at root level
Related
This question already has answers here:
Implementing pagination in mongodb
(2 answers)
How does MongoDB sort records when no sort order is specified?
(2 answers)
Closed 5 years ago.
I'm running mongo 3.4 (w/ wiredtiger). Up to now I have been using the 'fast pagination' strategy specified in the following article (https://scalegrid.io/blog/fast-paging-with-mongodb), namely:
Retrieve the _id of the last document in the current page
Retrieve documents greater than this “_id” in the next page
//Page 1
db.users.find().limit(pageSize);
//Find the id of the last document in this page
last_id = ...
//Page 2
users = db.users.find({'_id'> last_id}). limit(10);
//Update the last id with the id of the last document in this page
last_id = ...
I am about to shard my collection in order to allow horizontal scaling. As part of enabling sharding, I am going to use a unique composite key (on fields "user_id" and "post_id") for a shard key. This will guarantee document uniquness across shards, and should allow for relatively good document distribution across shards.
But after I shard my collection, will I be able to use the above fast-pagination strategy? If not, is there a common solution?
Thanks
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 6 years ago.
Is it possible in mongoDB to query documents where a field's value is greather than another one of the same document ?
I want to query each document where a > b for documents as:
{
a: Number,
b: Number,
}
Is it possible to reference a field's value in query to another one ?
Yes it is possible. Consider that you are trying to get the documents where a is greater than b. The code below is doing that.
db.collectionName.find({$where: "this.a > this.b"})
This question already has answers here:
Advantage of a unique index in MongoDB
(2 answers)
Closed 7 years ago.
I need to create a unique index on a field in mongoDB in order to prevent duplicates in my collection. I also want to create a single-field index on that same field in order to optimize for queries.
Do I need to create these two different indexes? Or will the unique index be used for queries as well?
Any help is appreciated!
The unique index will be used for queries, so the extra index is unnecessary.
You can test this, by looking at the indexes considered in the output from explain in the shell.
This question already has answers here:
MongoDb query condition on comparing 2 fields
(4 answers)
Closed 8 years ago.
Is it possible to find all documents in a collection where documents with field (_id) values exist in another field (ex. parentID). Taking in consideration that both fields exist in the documents of the same collection? Thanks
Categories.find({'_id': 'parentID'})
{
_id: 11,
parentID: 1
}
I am using MongoDB 2.6.7
Yes, this is easy to do with the $where operator.
db.Categories.find({'$where':"this._id === this.parent"})
This gives you more flexibility than your regular find syntax but be warned that MongoDB needs to evaluate the Javascript so this is slower than a regular query.
This question already has answers here:
How to update the _id of one MongoDB Document?
(7 answers)
Closed 8 years ago.
I am working on a project using MongoDB and storing users data with their email as _id field.
Now realizing that that is a stupid idea, I wanna add an extra field, let's say "userid" as the new _id field of this collection and let the email as regular field.
Is it possible to do that?
Thank you
It is not possible tp update the _id of a document.
Currently (and probably for the forseeable future) the only way to modify the _id field is to actually remove your old document and reinsert with a new _id:
// Insert our test doc
db.c.insert({_id:1,name:'sammaye'});
// Get it back out
doc=db.c.find({name:'sammaye'});
// Now lets update by removing it first
db.c.remove({_id:doc._id});
// Change the old _id to a new one
doc._id=2;
// Reinsert it and now it has new _id
db.c.insert(doc);