Let us create a collection with documents −
> db.demo151.insertOne({"ListOfNames":["Chris","David","Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3513b6fdf09dd6d08539da") } > db.demo151.insertOne({"ListOfNames":["Mike","Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3513c4fdf09dd6d08539db") } > db.demo151.insertOne({"ListOfNames":["John","David","Chris"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3513dcfdf09dd6d08539dc") }
Display all documents from a collection with the help of find() method −
> db.demo151.find();
This will produce the following output −
{ "_id" : ObjectId("5e3513b6fdf09dd6d08539da"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e3513c4fdf09dd6d08539db"), "ListOfNames" : [ "Mike", "Bob" ] } { "_id" : ObjectId("5e3513dcfdf09dd6d08539dc"), "ListOfNames" : [ "John", "David", "Chris" ] }
Following is the query to projection result as an array of selected items −
> db.demo151.distinct('_id', {'ListOfNames' : "Mike"});
This will produce the following output −
[ ObjectId("5e3513b6fdf09dd6d08539da"), ObjectId("5e3513c4fdf09dd6d08539db") ]