Found 1659 Articles for Big Data Analytics

MongoDB query to insert but limit the total records

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

206 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

73 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

103 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

95 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

505 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

674 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

211 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

228 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

Advertisements