MongoDB exact array matching



For exact array matching, simply use find() in MongoDB. Let us create a collection with documents −

> db.demo300.insertOne({"Values":[100,200,400]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e4d69d05d93261e4bc9ea4d")
}
> db.demo300.insertOne({"Values":[500,700,900,1000]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e4d69dd5d93261e4bc9ea4e")
}
> db.demo300.insertOne({"Values":[340,670,450,500]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e4d69eb5d93261e4bc9ea4f")
}

Display all documents from a collection with the help of find() method −

> db.demo300.find();

This will produce the following output −

{ "_id" : ObjectId("5e4d69d05d93261e4bc9ea4d"), "Values" : [ 100, 200, 400 ] }
{ "_id" : ObjectId("5e4d69dd5d93261e4bc9ea4e"), "Values" : [ 500, 700, 900, 1000 ] }
{ "_id" : ObjectId("5e4d69eb5d93261e4bc9ea4f"), "Values" : [ 340, 670, 450, 500 ] }

Here is the query to MongoDB array matching −

> db.demo300.find({"Values" : [ 340, 670, 450, 500 ]});

This will produce the following output −

{ "_id" : ObjectId("5e4d69eb5d93261e4bc9ea4f"), "Values" : [ 340, 670, 450, 500 ] }
raja
Published on 01-Apr-2020 07:24:07
Advertisements