AmitDiwan has Published 10744 Articles

Field selection within MongoDB query using dot notation?

AmitDiwan

AmitDiwan

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

475 Views

For this, use dot notation for field selection in MongoDB find(). Let us create a collection with documents −> db.demo302.insertOne({"Id":101, "details":[{"Name":"Chris", Age:21, "Subject":"MySQL"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d746f5d93261e4bc9ea52") } > db.demo302.insertOne({"Id":102, "details":[{"Name":"Bob", Age:23, "Subject":"MongoDB"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4d74815d93261e4bc9ea53") } > db.demo302.insertOne({"Id":103, ... Read More

MongoDB query to change order of array elements?

AmitDiwan

AmitDiwan

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

603 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

382 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

543 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

298 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

256 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

122 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

176 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

329 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

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

Advertisements