My sample record:
[
{
"name": "name1",
"status": "one",
"unit": [
],
"child": [],
"element": [
"6604"
],
"tags": [
"tag1",
"tag2",
"tag3"
]
},
{
"name": "name2",
"status": "one",
"unit": [
],
"child": [],
"element": [
"6604"
],
"tags": [
"tag1",
"tag2",
"tag3",
"tag4"
]
}
]
I tried in this way to get
tagsdata = dbname.distinct(‘tags.values’)
expecting the output: "tag1","tag2","tag3","tag4"
All unique values form the key field.
2
Answers
add
unique:true
In mongoDb model and while inserting data do check if data doesnt exists with same name with :
DB.findOne
MongoDB is a document database. It will never return a bare string or a list of strings.
Using aggregation you might
That pipeline might look like:
Which returns:
Playground