Found 6705 Articles for Database

MongoDB regular expression to match a specific record?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

139 Views

Let us first create a collection with documents −> dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "John" }, "StudentAge":21 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf227acb64a577be5a2bc07") } > dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "JOHN" }, "StudentAge":19 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf227b8b64a577be5a2bc08") } > dbworkingOfRegularExpressionDemoinsertOne({ "StudentDetails" : { "StudentName" : "Carol" }, "StudentAge":20 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf227c2b64a577be5a2bc09") }Following is the query to display all documents from a collection with the help of find() method −> dbworkingOfRegularExpressionDemofind();This will produce the following document −{ "_id" : ObjectId("5cf227acb64a577be5a2bc07"), "StudentDetails" : ... Read More

Delete specific record from an array nested within another array in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

191 Views

To delete specific record, use $pull operator. Let us first create a collection with documents −> dbdeletingSpecificRecordDemoinsertOne(    {       "StudentDetails": [          {             "StudentName": "John",             "StudentSubjectDetails": [                {                   "Subject": "MongoDB",                   "Marks":45                },                {                   "Subject": ... Read More

Specify return format in MongoDB to return the values as an array?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

216 Views

Use aggregation for this and add the values to an array using the $group and $addToSet operatorLet us first create a collection with documents −> dbspecifyReturnFormatDemoinsertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd364ef71edecf6a1f6c0") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd369ef71edecf6a1f6c1") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd36fef71edecf6a1f6c2") }Following is the query to display all documents from a collection with the help of find() method −> dbspecifyReturnFormatDemofind();Output{ "_id" : ObjectId("5cefd364ef71edecf6a1f6c0"), "Subject" : "MongoDB" } { "_id" : ObjectId("5cefd369ef71edecf6a1f6c1"), "Subject" : "MySQL" } { "_id" : ObjectId("5cefd36fef71edecf6a1f6c2"), "Subject" : ... Read More

Update key value where another key equals some value in MongoDB?

George John
Updated on 30-Jul-2019 22:30:26

229 Views

Use $elemMatch fir this with $setLet us first create a collection with documents −> dbkeyValueDemoinsertOne(    {       "_id" : new ObjectId(),       "CustomerDetails" : [          {             "Name" : "Chris",             "Age" :24,          },          {             "Name" : "Robert",             "Age" :29,          },          {             "Name" : "David",     ... Read More

Is it possible to return a list of specific values from a MongoDB query?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

182 Views

Yes, using map(). Let us first create a collection with documents −> dblistOfSpecificValuesDemoinsertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc8fef71edecf6a1f6bb") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc94ef71edecf6a1f6bc") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc98ef71edecf6a1f6bd") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc9cef71edecf6a1f6be") }Following is the query to display all documents from a collection with the help of find() method −> dblistOfSpecificValuesDemofind();Output{ "_id" : ObjectId("5cefcc8fef71edecf6a1f6bb"), "StudentName" : "John" } { "_id" : ObjectId("5cefcc94ef71edecf6a1f6bc"), "StudentName" : "Chris" } { "_id" : ObjectId("5cefcc98ef71edecf6a1f6bd"), "StudentName" : "Robert" } ... Read More

Which MongoDB query finds same value multiple times in an array?

Arjun Thakur
Updated on 30-Jul-2019 22:30:26

129 Views

You can use $where operator along with some script.Let us first create a collection with documents −> dbsameValueMultipleTimesDemoinsertOne(    {       "ListOfPrice":[          {"Price": 110},          {"Price":130},          {"Price": 145}       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc4e6ef71edecf6a1f6b9") } > dbsameValueMultipleTimesDemoinsertOne(    {       "ListOfPrice":[          {"Price": 110},          {"Price":178},          {"Price": 110}       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc4e7ef71edecf6a1f6ba") }Following ... Read More

MongoDB query to select one field if the other is null and the first field if both are not null?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

446 Views

For this, use $ifNull operatorLet us first create a collection with documents −> dbquerySelectDemoinsertOne({"Value1":10, "Value2":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0ceef71edecf6a1f6b6") } > dbquerySelectDemoinsertOne({"Value1":null, "Value2":30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0d7ef71edecf6a1f6b7") } > dbquerySelectDemoinsertOne({"Value1":60, "Value2":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0e2ef71edecf6a1f6b8") }Display all documents from a collection with the help of find() method −> dbquerySelectDemofind()pretty();Output{    "_id" : ObjectId("5cefc0ceef71edecf6a1f6b6"),    "Value1" : 10,    "Value2" : null } {    "_id" : ObjectId("5cefc0d7ef71edecf6a1f6b7"),    "Value1" : null,    "Value2" : 30 } {    "_id" : ObjectId("5cefc0e2ef71edecf6a1f6b8"),    "Value1" : 60, ... Read More

MongoDB query to search for Month and Day only?

George John
Updated on 30-Jul-2019 22:30:26

541 Views

To search for month and day only, use the aggregate framework.Let us first create a collection with documents −> db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2019-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcaeef71edecf6a1f6b2") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-30")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcb7ef71edecf6a1f6b3") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcbcef71edecf6a1f6b4") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2016-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbdaaef71edecf6a1f6b5") }Display all documents from a collection with the help of find() method −> db.monthAndDayDemo.find();Output{ "_id" : ObjectId("5cefbcaeef71edecf6a1f6b2"), "LoginDate" : ISODate("2019-04-27T00:00:00Z") } { "_id" : ObjectId("5cefbcb7ef71edecf6a1f6b3"), "LoginDate" : ISODate("2018-04-30T00:00:00Z") } { "_id" ... Read More

Limit number of values in a field using MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

347 Views

To limit number of values in a field, use $slice operator.Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to limit number of values in a field using MongoDB −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": 2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200 ] }

Display the last two values from field with MongoDB

George John
Updated on 30-Jul-2019 22:30:26

96 Views

Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to get the last two values.Here, we have used -ve sign under $slice −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": -2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 1000, 98 ] }

Advertisements