Discussion:
[mongodb-user] Distinct() on indexed field returns undefined
e***@hrs-pd.de
2018-02-02 13:57:14 UTC
Permalink
Hi,

In one collection we have documents which look like this:

{
"A" : [],
}

OR

{
"A" : [
{
"_id" : 1,
"Name" : "ASDF"
},
{
"_id" : 2,
"Name" : "GHJKL"
}
],
}



So we need all the distinct values of this array for our UI.
Running
db.getCollection('X').distinct('A')

actually works fine and returns the values we expect. If we then add an
index on this Field ( db.X.createIndex({"A":1,{Name:"IX_A"}) and run this
query again, there is suddenly an item undefined:
[
undefined,
{
"_id" : 1,
"Name" : "ASDF"
}, ...
]



So how can we get rid of this undefined item in the index?

Thanks!
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/0589a6f5-4a8e-4c63-99e3-b954e69f30c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
'Wan Bachtiar' via mongodb-user
2018-03-09 03:13:00 UTC
Permalink
actually works fine and returns the values we expect. If we then add an
index on this Field ( db.X.createIndex({“A”:1,{Name:”IX_A”}) and run this
query again, there is suddenly an item undefined:

Hi,

Depending on your use case, Partial Indexes
<https://docs.mongodb.com/manual/core/index-partial/> is likely what you’re
after.
For example you can create:

db.collection.createIndex({"A":1}, {partialFilterExpression:{"A":{"$exists":true}}})

Regards,
Wan.
​
--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.

For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+***@googlegroups.com.
To post to this group, send email to mongodb-***@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/3a149fca-1fa1-43de-863c-9d706edfcb83%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...