Found 6705 Articles for Database

How to count and sum a field between 2 dates in MongoDB?

AmitDiwan
Updated on 27-Mar-2020 11:37:24

1K+ Views

Use aggregation $gte and $lte along with $sum to count and sum a field between 2 dates. Let us first create a collection with documents −> db.countandsumdemo.insertOne({"Value":10, "created_at":ISODate('2019-10-11')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e6df5e889d7a51994fa") } > db.countandsumdemo.insertOne({"Value":50, "created_at":ISODate('2019-01-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e77f5e889d7a51994fb") } > db.countandsumdemo.insertOne({"Value":100, "created_at":ISODate('2019-06-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038e8af5e889d7a51994fc") }Following is the query to display all documents from a collection with the help of find() method −> db.countandsumdemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e038e6df5e889d7a51994fa"),    "Value" : 10,    "created_at" : ISODate("2019-10-11T00:00:00Z") ... Read More

Build index in the background with MongoDB

AmitDiwan
Updated on 27-Mar-2020 11:32:25

3K+ Views

To create index in the background, use createIndex() method and set “background: true” as in the below syntax −db.yourCollectionName.createIndex({"yourFieldName1":1,"yourFieldName2":1},{background: true} );Let us implement the above syntax in order to create index and set background −> db.indexCreationDemo.createIndex({"StudentName":1,"StudentAge":1},{background: true} ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Let us display the indexes −> db.indexCreationDemo.getIndexes();This will produce the following output −[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       "ns" : "web.indexCreationDemo"    },    {       "v" : 2,       "key" : {          "StudentName" : 1,          "StudentAge" : 1       },       "name" : "StudentName_1_StudentAge_1",       "ns" : "web.indexCreationDemo",       "background" : true    } ]

Fix: MongoDB Robomongo: db.data.find(…).collation is not a function?

AmitDiwan
Updated on 27-Mar-2020 11:29:35

110 Views

The collation introduced in version MongoDB 3.4. Maybe, you implemented collation in a previous version.For our example, we are using MongoDB version 4.0.5. Following is the query to check the current version on system −> db.version()This will produce the following output −4.0.5Let us first create a collection with documents −> db.collationExample.createIndex({Value: 1}, {collation: {locale: "en", strength: 1}}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.collationExample.insertOne({'Value':'x'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a3cf5e889d7a51994f5") } > db.collationExample.insertOne({'Value':'X'}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038a48f5e889d7a51994f6") } > ... Read More

Calculating average value per document with sort in MongoDB?

AmitDiwan
Updated on 27-Mar-2020 11:26:56

199 Views

To calculate average, use aggregate along with $avg. Let us first create a collection with documents −> db.calculateAverage.insertOne({'Value':[10, 20, 80]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383e3f5e889d7a51994dc") } > db.calculateAverage.insertOne({'Value':[12, 15, 16]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383edf5e889d7a51994dd") } > db.calculateAverage.insertOne({'Value':[30, 35, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0383f5f5e889d7a51994de") }Following is the query to display all documents from a collection with the help of find() method −> db.calculateAverage.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0383e3f5e889d7a51994dc"),    "Value" : [       10,       20,     ... Read More

Promote subfields to top level in projection without listing all keys in MongoDB?

AmitDiwan
Updated on 27-Mar-2020 11:23:46

180 Views

To promote subfields to top level in projection, use $objectToArray and $arrayToObject. Let us first create a collection with documents:> db.promoteSubfieldsDemo.insertOne({'s':10, 'y':{'t':20, 'u':30, }}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e038004190a577c668b55d5") }Following is the query to display all documents from a collection with the help of find() method −> db.promoteSubfieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e038004190a577c668b55d5"),    "s" : 10,    "y" : {       "t" : 20,       "u" : 30    } }Following is the query to promote subfields to top level in projection without listing all keys ... Read More

Deleting specific record from an array nested within another array in MongoDB

AmitDiwan
Updated on 27-Mar-2020 11:14:03

690 Views

To delete specific record, use “$pull” and since we are updating the already created collection, use UPDATE().Let us create a collection with documents −> db.demo213.insertOne({ ...   "id": 101, ...   "details1": [ ...      { ...         "Name": "Chris", ...         "details2": [ ...            { ...               "StudentName": "David", ...               "Subject": "MongoDB" ...            }, ...            { ...               ... Read More

Finding a MongoDB document through a word

AmitDiwan
Updated on 27-Mar-2020 11:09:12

171 Views

To find a MongoDB document through a word, use find() and set the word like −word/iLet us create a collection with documents −> db.demo212.insertOne({"details":[{"Name":"John Doe"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e2c7603d395bdc21346ff") } > db.demo212.insertOne({"details":[{"Name":"Chris Brown"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e2c8003d395bdc2134700") } > db.demo212.insertOne({"details":[{"Name":"Robert doe"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e2c8a03d395bdc2134701") }Display all documents from a collection with the help of find() method −> db.demo212.find();This will produce the following output −{ "_id" : ObjectId("5e3e2c7603d395bdc21346ff"), "details" : [ { "Name" : "John Doe" } ] } { "_id" : ObjectId("5e3e2c8003d395bdc2134700"), "details" : ... Read More

How do you test if two external values are equal in a MongoDB criteria object?

AmitDiwan
Updated on 27-Mar-2020 11:07:25

88 Views

To test the values, use $type. Let us create a collection with documents −> db.demo211.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e298203d395bdc21346fa") } > db.demo211.insertOne({id:102, "Name":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3e2a5403d395bdc21346fb") }Display all documents from a collection with the help of find() method −> db.demo211.find();This will produce the following output −{ "_id" : ObjectId("5e3e298203d395bdc21346fa"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e3e2a5403d395bdc21346fb"), "id" : 102, "Name" : null }Following is the query to test if two external values are equal in a MongoDB criteria object −> v1=200; 200 > v2=200; ... Read More

Get the count of a specific value in MongoDB

AmitDiwan
Updated on 27-Mar-2020 11:05:25

406 Views

To get the count of a specific value in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo210.insertOne( ...   { ...      details: [ ...         { ...            ClientName: "Robert" ...         }, ...         { ... ...            lientName: "John Doe" ...         }, ...         { ... ...            ClientName: "Robert" ...         }, ...         { ... ... ... Read More

MongoDB query to convert the field value and create datetime day of month during projection?

AmitDiwan
Updated on 27-Mar-2020 10:58:16

290 Views

To convert filed value to create datetime day of month, use MongoDB aggregate(). Let us create a collection with documents −> db.demo209.insertOne( ...   { ...      "_id" : "101", ...      "details" : [ ...         { ...            "dat" : 1528929908, ...            "Name" : "Chris" ...         }, ...         { ...            "dat" : 1529082069, ...            "Name":"Carol" ...         } ...      ], ... ... Read More

Advertisements