AmitDiwan has Published 10740 Articles

MongoDB query to change order of array elements?

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 07:26:34

623 Views

Use swap concept to change the order of array elements. Let us create a collection with documents −> db.demo301.insertOne({"Name":["Chris", "David", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d6ff55d93261e4bc9ea51") }Display all documents from a collection with the help of find() method −> db.demo301.find();This will produce the following output −{ ... Read More

MongoDB exact array matching

AmitDiwan

AmitDiwan

Updated on 01-Apr-2020 07:24:30

411 Views

For exact array matching, simply use find() in MongoDB. Let us create a collection with documents −> db.demo300.insertOne({"Values":[100, 200, 400]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d69d05d93261e4bc9ea4d") } > db.demo300.insertOne({"Values":[500, 700, 900, 1000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d69dd5d93261e4bc9ea4e") } > db.demo300.insertOne({"Values":[340, 670, 450, 500]}); ... Read More

MongoDB query to access an object in an array

AmitDiwan

AmitDiwan

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

570 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"] ...   ... Read More

MongoDB transaction & indexes for duplicate values

AmitDiwan

AmitDiwan

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

321 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" : ... Read More

MongoDB query to insert but limit the total records

AmitDiwan

AmitDiwan

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

274 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, ... Read More

Native Querying MongoDB inside array and get the count

AmitDiwan

AmitDiwan

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

144 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":[ ...         { ...           ... Read More

Querying object's field array values in MongoDB?

AmitDiwan

AmitDiwan

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

193 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 ... Read More

How do I query a MongoDB collection?

AmitDiwan

AmitDiwan

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

353 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, ... Read More

MongoDB: combining AND and OR?

AmitDiwan

AmitDiwan

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

677 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") } ... Read More

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

AmitDiwan

AmitDiwan

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

143 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,   ... Read More

Advertisements