Answer the question
In order to leave comments, you need to log in
How to index a collection by unique values?
There is a collection, for example -
[
{userID:"111111", content:"text"}, {userID:"222222", content:"text"},
{userID:"111111", content:"text"}, { userID:"111111", content:"text"},
{userID:"222222", content:"text"}, {userID:"333333", content:"text"},
{userID:"333333", content: "text"}, {userID:"111111", content:"text"}
]
Elements with the same userID need to be indexed separately. Those. the result will be -
[
{userID:"111111", content:"text", index:0}, {userID:"222222", content:"text", index:0},
{userID:"111111", content: "text"
{userID:"333333", content:"text", index:1}, {userID:"111111", content:"text", index:3}
]
I know there is a .createIndex function, but how do I compose a filter/ configure it so that it indexes all userIDs separately?
You can, of course, use
.createIndex({userID:"111111"})
.createIndex({userID:"222222"})
.createIndex({userID:"333333"})
We will get the desired result, but there are about 2000 unique id in the collection and doing it this way is not the most rational solution.
Answer the question
In order to leave comments, you need to log in
The thing is that I need to get three randomly selected "content" values, for a certain userIDSo you need a regular index, not a unique one.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question