Set $gt condition in MongoDB $and

MongoDBDatabaseBig Data Analytics

The $and performs a logical AND operation on an array of one or more expressions. Let us create a collection with documents −

> db.demo680.insertOne({Values:40});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea4461b04263e90dac943fe")
}
> db.demo680.insertOne({Values:70});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea4461e04263e90dac943ff")
}
> db.demo680.insertOne({Values:[80,30]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea4462a04263e90dac94400")
}
> db.demo680.insertOne({Values:20});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ea4463304263e90dac94401")
}

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

> db.demo680.find();

This will produce the following output −

{ "_id" : ObjectId("5ea4461b04263e90dac943fe"), "Values" : 40 }
{ "_id" : ObjectId("5ea4461e04263e90dac943ff"), "Values" : 70 }
{ "_id" : ObjectId("5ea4462a04263e90dac94400"), "Values" : [ 80, 30 ] }
{ "_id" : ObjectId("5ea4463304263e90dac94401"), "Values" : 20 }

Following is the query to use $gt in $and −

> db.demo680.find({$and : [{Values:{$gt:60}}, {Values:30}]})

This will produce the following output −

{ "_id" : ObjectId("5ea4462a04263e90dac94400"), "Values" : [ 80, 30 ] }
raja
Published on 13-May-2020 10:17:11
Advertisements