Found 1659 Articles for Big Data Analytics

How to index and sort with pagination using custom field in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 13:02:12

324 Views

Let us first create a collection with documents −> db.demo373.createIndex({"Name":1, "CountryName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo373.insertOne({"Name":"Chris", "Age":22, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffde2ae06a1609a00aff") } > db.demo373.insertOne({"Name":"David", "Age":21, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffe82ae06a1609a00b00") } > db.demo373.insertOne({"Name":"Bob", "Age":23, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59fff42ae06a1609a00b01") } > db.demo373.insertOne({"Name":"John", "Age":21, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e59ffff2ae06a1609a00b02") } > db.demo373.insertOne({"Name":"Carol", "Age":23, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5a00082ae06a1609a00b03") }Display all ... Read More

How to use $ifNull with MongoDB aggregation?

AmitDiwan
Updated on 02-Apr-2020 13:00:31

1K+ Views

The $ifNull evaluates an expression and returns the value of the expression if the expression evaluates to a non-null value.Let us first create a collection with documents −> db.demo372.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591aea2ae06a1609a00af6") } > db.demo372.insertOne({"FirstName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591aef2ae06a1609a00af7") } > db.demo372.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591af42ae06a1609a00af8") } > db.demo372.insertOne({"FirstName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e591afb2ae06a1609a00af9") }Display all documents from a collection with the help of find() method −> db.demo372.find();This will produce the following output −{ "_id" : ObjectId("5e591aea2ae06a1609a00af6"), "FirstName" : "Chris" ... Read More

Changing the primary key on a MongoDB collection?

AmitDiwan
Updated on 02-Apr-2020 12:59:29

1K+ Views

To change the primary key, you need to first delete it. Use forEach() along with delete to remove and then get a new primary key. Let us create a collection with documents −> db.demo41.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25ce4acfb11e5c34d898e3") }Display all documents from a collection with the help of find() method −> db.demo41.find();This will produce the following output −{ "_id" : ObjectId("5e25ce4acfb11e5c34d898e3"), "StudentName" : "Carol" }Here is the query to change the primary key on a MongoDB collection −> var next = db.demo41.find() > > next.forEach(function(s) { ...    var prevId=s._id; ...    delete s._id; ... ... Read More

How to create a new object and get its saved object in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 12:57:56

1K+ Views

For this, use save() in MongoDB. Following is the syntax −var anyVaribaleName=yourValue db.anyCollectionName.save(yourVariableName); yourVariableName;Let us first create an object for our example −> var studentDetails={"StudentName":"Chris","ListOfMarks":[56,78,89],"ListOfSubject":["MySQL","Java","MongoDB"]};Let us save the above created object “studentDetails” −> db.demo40.save(studentDetails); WriteResult({ "nInserted" : 1 })Let us display the value −> studentDetails;This will produce the following output −{    "StudentName" : "Chris",    "ListOfMarks" : [       56,       78,       89    ],    "ListOfSubject" : [       "MySQL",       "Java",       "MongoDB"    ],    "_id" : ObjectId("5e177757cfb11e5c34d898e2") }

Search by specific field in MongoDB

AmitDiwan
Updated on 02-Apr-2020 12:58:08

260 Views

Let us first create a collection with documents −> db.demo371.insertOne({"Name":"David", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f6982ae06a1609a00af2") } > db.demo371.insertOne({"Name":"John", "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f69e2ae06a1609a00af3") } > db.demo371.insertOne({"Name":"Bob", "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f6a42ae06a1609a00af4") } > db.demo371.insertOne({"Name":"Mike", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57f6ba2ae06a1609a00af5") }Display all documents from a collection with the help of find() method −> db.demo371.find();This will produce the following output −{ "_id" : ObjectId("5e57f6982ae06a1609a00af2"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e57f69e2ae06a1609a00af3"), "Name" : "John", "CountryName" : "UK" } ... Read More

Display collections in a particular MongoDB database?

AmitDiwan
Updated on 02-Apr-2020 12:55:58

103 Views

At first, switch to a particular database in MongoDB with the USE command as in the below syntax −use yourDatabaseName; db.getCollectionNames();Let us implement the above syntax to display collections of database WEB −> use web; switched to db web > db.getCollectionNames();This will produce the following output −[    "2015-myCollection",    "2015-yourCollection",    "2019-employeeCollection",    "addColumnDemo",    "applyConditionDemo",    "calculateAverage",    "calculateSumOfDocument",    "changeSimpleFieldDemo",    "check",    "checkFieldDemo",    "collationExample",    "compoundIndexDemo",    "countandsumdemo",    "creatingAliasDemo",    "decreasetimeusingindex",    "demo1",    "demo10",    "demo11",    "demo12",    "demo13",    "demo14",    "demo15",    "demo16",    "demo17",    "demo18", ... Read More

Update a single list item of a Mongo DB document and increment it by 1

AmitDiwan
Updated on 02-Apr-2020 12:52:40

60 Views

For this, use positional operator($). To increment a field value by 1, use $inc operator. Let us create a collection with documents −>db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-1", "ProductPrice":349}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d54cfb11e5c34d898df") } >db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-2", "ProductPrice":998}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d61cfb11e5c34d898e0") } >db.demo39.insertOne({"ProductDetails":[{"ProductName":"Product-3", "ProductPrice":145}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176d6acfb11e5c34d898e1") }Display all documents from a collection with the help of find() method −> db.demo39.find();This will produce the following output −{ "_id" : ObjectId("5e176d54cfb11e5c34d898df"), "ProductDetails" : [ { "ProductName" : "Product-1", "ProductPrice" : 349 } ] } { "_id" : ObjectId("5e176d61cfb11e5c34d898e0"), "ProductDetails" : [ ... Read More

MongoDB query for documents matching array, irrespective of elements order

AmitDiwan
Updated on 02-Apr-2020 12:56:10

170 Views

For this, use $all in MongoDB. The $all operator in MongoDB selects the documents where the value of a field is an array that contains all the specified elements.Let us first create a collection with documents −> db.demo370.insertOne( ...    { ...       "Name" : "Chris", ...       "details" : [ ...          { ...             "Subject" : "MySQL", ...             "CountryName" : "US" ...          }, ...          { ...             ... Read More

Retrieving array values from a find query in MongoDB database

AmitDiwan
Updated on 02-Apr-2020 12:51:13

68 Views

To retrieve array values from a find query, use dot notation. Let us create a collection with documents −> db.demo38.insertOne({"ClientDetails":[{"ClientId":101, "ClientName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176abccfb11e5c34d898d9") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":102, "ClientName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176ac7cfb11e5c34d898da") } > db.demo38.insertOne({"ClientDetails":[{"ClientId":103, "ClientName":"Mike"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e176ad0cfb11e5c34d898db") }Display all documents from a collection with the help of find() method −> db.demo38.find();This will produce the following output −{ "_id" : ObjectId("5e176abccfb11e5c34d898d9"), "ClientDetails" : [ { "ClientId" : 101, "ClientName" : "Chris" } ] } { "_id" : ObjectId("5e176ac7cfb11e5c34d898da"), "ClientDetails" : [ { ... Read More

MongoDB query to convert string to int?

AmitDiwan
Updated on 02-Apr-2020 12:50:17

2K+ Views

To convert string to int, use parseInt() in MongoDB. Let us first create a collection with documents −> db.demo369.insertOne({"Price":"1000000"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2e32ae06a1609a00aed") } > db.demo369.insertOne({"Price":"1747864"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2e92ae06a1609a00aee") } > db.demo369.insertOne({"Price":"19548575"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57e2ee2ae06a1609a00aef") }Display all documents from a collection with the help of find() method −> db.demo369.find();This will produce the following output −{ "_id" : ObjectId("5e57e2e32ae06a1609a00aed"), "Price" : "1000000" } { "_id" : ObjectId("5e57e2e92ae06a1609a00aee"), "Price" : "1747864" } { "_id" : ObjectId("5e57e2ee2ae06a1609a00aef"), "Price" : "19548575" }Following is the query to convert ... Read More

Advertisements