Found 6702 Articles for Database

MongoDB query to access an object in an array

AmitDiwan
Updated on 01-Apr-2020 07:23:32

470 Views

To access an object in an array, use dot notation. Let us create a collection with documents −> db.demo299.insertOne( ...   { ...      "id":100, ...      "Name":"Robert", ...      "details":[ ...         { ...            "SubjectName":["C++", "Python"] ...         }, ...         { ...            "SubjectName":["Spring", "Hibernate"] ...         } ...      ] ...   } ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d685a5d93261e4bc9ea4b") } > > > db.demo299.insertOne( ...   { ... ... Read More

MongoDB transaction & indexes for duplicate values

AmitDiwan
Updated on 01-Apr-2020 07:20:37

223 Views

Use ensureIndex() and set unique:1 inside the array. Let us create a collection with documents −> db.demo298.save({Name: 'Chris', Marks: [46, 79] }); WriteResult({ "nInserted" : 1 }) > db.demo298.save({Name: 'David', Marks: [67, 88] }); WriteResult({ "nInserted" : 1 }) > db.demo298.ensureIndex({ Marks: 1 }, {unique: 1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo298.save({Name: 'Mike', Marks: [88, 98] }); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate key error collection: test.demo298 index: Marks_1 dup ... Read More

MongoDB query to insert but limit the total records

AmitDiwan
Updated on 01-Apr-2020 07:19:11

208 Views

To insert and limit the total records while inserting, use capped:true and set the size and max values.Let us create a collection with documents wherein we have set capped:true and size to 4 −> db.createCollection("demo297", {capped:true, size:4, max:4}); { "ok" : 1 } > db.demo297.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d54385d93261e4bc9ea43") } > db.demo297.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea44") } > db.demo297.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea45") } > db.demo297.insertOne({"Name":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d543f5d93261e4bc9ea46") } > db.demo297.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d54405d93261e4bc9ea47") ... Read More

Native Querying MongoDB inside array and get the count

AmitDiwan
Updated on 01-Apr-2020 07:17:50

75 Views

To query inside array and check for existence to get the count, use $exists. Let us create a collection with documents −> db.demo296.insertOne( ...   { ...      "id":101, ...      "Name":"Chris", ...      "details":[ ...         { ...            SubjectId:[101, 103], ...            "SubjectName":["MySQL", "MongoDB"] ...         }, ...         { ...            SubjectId:[102, 104], ...            "SubjectName":["Java", "C"] ...         } ...      ] ...   ... Read More

Querying object's field array values in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 07:14:41

104 Views

Query object’s field array value using arrayFieldName along with value. Let us create a collection with documents −> db.demo295.insertOne({"status":["Active", "Inactive"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d4ea65d93261e4bc9ea39") } > db.demo295.insertOne({"status":["Yes", "No"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d4eb15d93261e4bc9ea3a") }Display all documents from a collection with the help of find() method −> db.demo295.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e4d4ea65d93261e4bc9ea39"),    "status" : [       "Active",       "Inactive"    ] } {    "_id" : ObjectId("5e4d4eb15d93261e4bc9ea3a"),    "status" : [       "Yes",       "No"    ] ... Read More

How do I query a MongoDB collection?

AmitDiwan
Updated on 01-Apr-2020 07:12:31

265 Views

To query or return a MongoDB collection, use getCollection(). Let us create a collection with documents −> db.demo294.insertOne({"EmployeeId":101, "EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d4a1a5d93261e4bc9ea36") } > db.demo294.insertOne({"EmployeeId":102, "EmployeeName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d4a255d93261e4bc9ea37") } > db.demo294.insertOne({"EmployeeId":103, "EmployeeName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d4a335d93261e4bc9ea38") }Display all documents from a collection with the help of find() method −> db.demo294.find();This will produce the following output −{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a255d93261e4bc9ea37"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" ... Read More

MongoDB: combining AND and OR?

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

544 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

96 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

507 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

676 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

Advertisements