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.
Related
This question already has answers here:
Storing null vs not storing the key at all in MongoDB
(5 answers)
Closed 3 months ago.
{
A: 1
B: null
}
vs
{
A: 1
}
Is there any differences between the 2 documents?
Of course there is a difference. Document with B: null consumes little more space on your disk. Also if you like to query them, they are different, see Or with If and In mongodb
Comment from #Takis is not fully correct. When you create an index in MongoDB, then it indexes also documents where the field does not exist - unless you define a Sparse Index/Partial Index. This behavior is different to most relational SQL databases.
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:
How to ORDER BY FIELD VALUE in MongoDB
(2 answers)
Closed 6 years ago.
I there a way to do ORDER BY FIELD in MongoDB?
In Mysql there is something like:
SELECT id, name, priority
FROM mytable
ORDER BY FIELD(priority, "core", "board", "other")
Can this be achieved in MongoDB?
Similar query is answered in : How to ORDER BY FIELD VALUE in MongoDB
This uses aggregate function. What I want is without aggregate and something with in find.
Not currently. MongoDB only allows to sort a field by normal old ascending and descending order. You cannot give a custom sort order of priority or anything.
The closest would be to use the aggregation framework to assign values to the field to sort the way you want. Though this will not work well with large queries so I would not recommend it.
Yes MongoDB has "Order By" functionality it is achieved using sort() see below for two primitive examples of how to use:
db.collection.find({}).sort({"fieldName" : 1})
For descending sort use:
db.collection.find({}).sort({"fieldName" : -1})
This question already has answers here:
MongoDB - how to query for a nested item inside a collection?
(3 answers)
Closed 9 years ago.
Consider my query to be: {cheese:"Cheddar"} and I have the following collections:
{vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Blue"}, {milk:"Chocolate}, {cheese:"Cheddar"}
How do I make a find that returns me all collections that include cheese:Cheddar?
The result would be {vegetable:"Lettuce", cheese:"Cheddar"}, {cheese:"Cheddar"} but right now it fives me just {cheese:"Cheddar"}. From what I investigated I only found tokens to work with arrays.
I do NOT know the name of the property is cheese, nor do I know if there are any other ingredients.
I am looking for a way to get documents from a collection, where the query is included in a field, by the names of the properties in the query and the respective values.
Using db.collection.findOne({cheese:"Cheddar"}) you will get as a result only one document, maybe {cheese:"Cheddar"} or maybe {vegetable:"Lettuce", cheese:"Cheddar"}, the first one that MongoDB finds depending on the _id field. If what you want is getting both, you should use db.collection.find({cheese:"Cheddar"}).
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