Found 6705 Articles for Database

MongoDB: combining AND and OR?

AmitDiwan
Updated on 31-Mar-2020 14:04:53

649 Views

Let us first create a collection with documents −> db.demo293.insertOne({FirstName:"Chris", LastName:"Brown", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d45075d93261e4bc9ea32") } > db.demo293.insertOne({FirstName:"David", LastName:"Miller", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d45265d93261e4bc9ea33") } > db.demo293.insertOne({FirstName:"John", LastName:"Smith", Age:24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d45385d93261e4bc9ea34") } > db.demo293.insertOne({FirstName:"Adam", LastName:"Doe", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d46cf5d93261e4bc9ea35") }Display all documents from a collection with the help of find() method −> db.demo293.find();This will produce the following output −{ "_id" : ObjectId("5e4d45075d93261e4bc9ea32"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e4d45265d93261e4bc9ea33"), ... Read More

MongoDB - How to check for equality in collection and in embedded document?

AmitDiwan
Updated on 31-Mar-2020 14:02:55

124 Views

For this, check using $where in MongoDB. Let us create a collection with documents −> db.demo292.insertOne({FirstName:"Chris", LastName:"Brown", ...   "Friend":{FirstName:"David", "LastName":"Miller"} ...   } ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c10aa5d93261e4bc9ea30") } > db.demo292.insertOne({FirstName:"John", LastName:"Doe", ...   "Friend":{FirstName:"Mike", "LastName":"Doe"} ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c10dc5d93261e4bc9ea31") }Display all documents from a collection with the help of find() method −>  db.demo292.find();This will produce the following output −{ "_id" : ObjectId("5e4c10aa5d93261e4bc9ea30"), "FirstName" : "Chris", "LastName" : "Brown", "Friend" : { "FirstName" : "David", "LastName" : "Miller" } } { "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", ... Read More

MongoDB - How to get the sum of two columns and save it to another column?

AmitDiwan
Updated on 31-Mar-2020 13:53:36

591 Views

To get the sum of two columns, use $add. Let us create a collection with documents −> db.demo291.insertOne({"Value1":10, "Value2":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c0e1e5d93261e4bc9ea2f") }Display all documents from a collection with the help of find() method −> db.demo291.find();This will produce the following output −{ "_id" : ObjectId("5e4c0e1e5d93261e4bc9ea2f"), "Value1" : 10, "Value2" : 50 }Following is the query to get the sum of two columns and save it to another column “Value3” −> db.demo291.aggregate( ...   { "$project" : { ...      'Value1' : '$Value1', ...      'Value2' : '$Value2', ...      'Value3' : ... Read More

MongoDB query for exact match

AmitDiwan
Updated on 31-Mar-2020 13:50:14

845 Views

For exact match, you can use $exists that checks for a match. Let us create a collection with documents −> db.demo290.insertOne({"ListOfName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c0c9e5d93261e4bc9ea2d") } > db.demo290.insertOne({"ListOfName":["Chris", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c0cb05d93261e4bc9ea2e") }Display all documents from a collection with the help of find() method −> db.demo290.find();This will produce the following output −{ "_id" : ObjectId("5e4c0c9e5d93261e4bc9ea2d"), "ListOfName" : "Chris" } { "_id" : ObjectId("5e4c0cb05d93261e4bc9ea2e"), "ListOfName" : [ "Chris", "David" ] }Here is the query for exact match of a value −> db.demo290.find({$and: [{'ListOfName.0': {$exists: false}}, {"ListOfName": 'Chris'}]});This will produce the ... Read More

Make an array of multiple arrays in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:48:43

256 Views

To make an array of multiple arrays, use $unwind in MongoDB aggregate. Let us create a collection with documents −> db.demo289.insertOne({"Section":["A", "B", "E"], "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c06fcf49383b52759cbc3") } > db.demo289.insertOne({"Section":["C", "D", "B"], "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c070af49383b52759cbc4") }Display all documents from a collection with the help of find() method −> db.demo289.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4c06fcf49383b52759cbc3"),    "Section" : [ "A", "B", "E" ],    "Name" : "Chris" } {    "_id" : ObjectId("5e4c070af49383b52759cbc4"),    "Section" : [ "C", "D", "B" ],    "Name" ... Read More

Implement MongoDB $push in embedded document array?

AmitDiwan
Updated on 31-Mar-2020 13:46:42

277 Views

Let us create a collection with documents −>db.demo288.insertOne({"Name":"Chris", details:[{"CountryName":"US", Marks:78}, {"CountryName":"UK", Marks:68}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c0393f49383b52759cbbe") }Display all documents from a collection with the help of find() method −> db.demo288.find();This will produce the following output −{ "_id" : ObjectId("5e4c0393f49383b52759cbbe"), "Name" : "Chris", "details" : [ { "CountryName" : "US", "Marks" : 78 }, { "CountryName" : "UK", "Marks" : 68 } ] }Following is the query to implement $push in embedded document array −> db.demo288.update( {Name: "Chris"}, {$push:{ "details":{"CountryName" : "AUS", "Marks" : 98} }}, {upsert:true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : ... Read More

MongoDB query to get only distinct values

AmitDiwan
Updated on 31-Mar-2020 13:45:10

4K+ Views

To get distinct values, use distinct() in MongoDB. It finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us create a collection with documents −> db.demo287.insertOne({"details":{"AllVowels":["a", "u", "u", "o", "e", "a", "o", "i"]}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4c014cf49383b52759cbbd") }Display all documents from a collection with the help of find() method −> db.demo287.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4c014cf49383b52759cbbd"),    "details" : {       "AllVowels" : [ "a", "u", "u", "o", "e", "a", "o", "i" ]    } }Following ... Read More

Min and max values of an array in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:43:39

1K+ Views

To get the min and max values, use $min and $max. Let us create a collection with documents −> db.demo286.insertOne({"details":[{Value1:70, Value2:50}, {Value1:30, Value2:36}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ac743f49383b52759cbbc") }Display all documents from a collection with the help of find() method −> db.demo286.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4ac743f49383b52759cbbc"),    "details" : [       {          "Value1" : 70,          "Value2" : 50       },       {          "Value1" : 30,          "Value2" : 36 ... Read More

What is the BSON query for the command 'show dbs' (list of databases) in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:41:29

92 Views

The command you can use is db.runCommand(). Let us first switch to admin −> use admin switched to db adminNow, run the command −> db.runCommand({listDatabases : 1})This will produce the following output −{    "databases" : [       {          "name" : "admin",          "sizeOnDisk" : 2375680,          "empty" : false       },       {          "name" : "app",          "sizeOnDisk" : 32768,          "empty" : false       },       {   ... Read More

MongoDB query to limit subdocuments having the given fields of the 'projection'

AmitDiwan
Updated on 31-Mar-2020 13:37:33

237 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo285.insertOne( ...   {... details : [ ...      { ...         Name : "Chris" ...      }, ...      { ...         Name2: "Bob" ...      }, ...      { ...         Name: "Mike" ...      } ...   ] ...   } ...) {    "acknowledged" : true,    "insertedId" : ObjectId("5e4abffef49383b52759cbb9") }Display all documents from a collection with the help of find() method −> db.demo285.find();This will produce the ... Read More

Advertisements