Found 1659 Articles for Big Data Analytics

Fetch specific documents with array values in MongoD

AmitDiwan
Updated on 11-May-2020 09:55:45

76 Views

To fetch specific documents, use limit() along with toArray(). The toArray() method returns an array that contains all the documents from a cursor. Let us create a collection with documents −> db.demo482.insertOne({_id:1, "StudentInformation":[{"Name":"Chris", "Age":21}]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo482.insertOne({_id:2, "StudentInformation":[{"Name":"Bob", "Age":23}]}); { "acknowledged" : true, "insertedId" : 2 } > db.demo482.insertOne({_id:3, "StudentInformation":[{"Name":"David", "Age":20}]}); { "acknowledged" : true, "insertedId" : 3 }Display all documents from a collection with the help of find() method −> db.demo482.find();This will produce the following output −{ "_id" : 1, "StudentInformation" : [ { "Name" : "Chris", "Age" : 21 } ] ... Read More

How to use save() correctly in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:54:33

357 Views

Use the db.collection.save() to update an existing document or inserts a new document, depending on its document parameter. Let us create a collection with documents −> db.demo481.save({"FirstName":"Chris", "LastName":"Brown"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"David", "LastName":"Miller"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John", "LastName":"Doe"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John", "LastName":"Smith"}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo481.find();This will produce the following output −{ "_id" : ObjectId("5e82db39b0f3fa88e227909c"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e82db45b0f3fa88e227909d"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ... Read More

Format date value in MongoDB shell?

AmitDiwan
Updated on 11-May-2020 09:52:55

438 Views

To format date value, use $dateToString in MongoDB. Let us create a collection with documents −> db.demo480.insertOne({id:1, "DueDate":new ISODate("2020-01-10")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e821056b0f3fa88e2279098") } > db.demo480.insertOne({id:1, "DueDate":new ISODate("2017-12-21")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e821062b0f3fa88e2279099") } > db.demo480.insertOne({id:1, "DueDate":new ISODate("2019-10-12")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82106ab0f3fa88e227909a") } > db.demo480.insertOne({id:1, "DueDate":new ISODate("2019-12-01")});{    "acknowledged" : true,    "insertedId" : ObjectId("5e821078b0f3fa88e227909b") }Display all documents from a collection with the help of find() method −> db.demo480.find();This will produce the following output −{ "_id" : ObjectId("5e821056b0f3fa88e2279098"), "id" : 1, "DueDate" : ISODate("2020-01- 10T00:00:00Z") } { "_id" : ... Read More

MongoDB query to add timestamp only if it is not present

AmitDiwan
Updated on 11-May-2020 09:52:21

221 Views

For this, use upsert and multi in MongoDB −Upsert − If set to true, creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found.Multi − f set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false.Let us create a collection with documents −> db.demo479.insertOne({"DueDate":new ISODate("2020-01-10"), "Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e820733b0f3fa88e2279094") } > db.demo479.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e820748b0f3fa88e2279095") } > db.demo479.insertOne({"DueDate":new ISODate("2019-12-31"), ... Read More

MongoDB $unwind to get the count

AmitDiwan
Updated on 11-May-2020 09:50:05

457 Views

The $unwind in MongoDB deconstructs an array field from the input documents to output a document for each element. Use $unwind along with aggregate() to get the count. Let us create a collection with documents −> db.demo478.insertOne( ... { ... ...    "Details" : { ...       _id:1, ...       "Information" : [ ...          { ...             "Name" : "Chris", ...             "Age":21 ...          }, ...          { ...             ... Read More

Get the real document BSON size in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:48:28

806 Views

You can use Object.bsonsize() to get real document size. It prints the BSON size of a document in bytes. Let us create a collection with documents −> db.demo477.insertOne({"ClientId":1, "ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82015fb0f3fa88e227908f") } > db.demo477.insertOne({"ClientId":2, "ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e820167b0f3fa88e2279090") } > db.demo477.insertOne({"ClientId":3, "ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82016db0f3fa88e2279091") }Display all documents from a collection with the help of find() method −> db.demo477.find();This will produce the following output −{ "_id" : ObjectId("5e82015fb0f3fa88e227908f"), "ClientId" : 1, "ClientName" : "Chris" } { "_id" : ObjectId("5e820167b0f3fa88e2279090"), "ClientId" : 2, "ClientName" : ... Read More

MongoDB query to update all documents matching specific IDs

AmitDiwan
Updated on 11-May-2020 09:46:17

675 Views

Use the updateMany() function to update all documents that match the filter criteria. Let us create a collection with documents −> db.demo476.insertOne({_id:1, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo476.insertOne({_id:2, "Name":"David"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo476.insertOne({_id:3, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo476.insertOne({_id:4, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo476.find();This will produce the following output −{ "_id" : 1, "Name" : "Chris" } { "_id" : 2, "Name" : "David" } { "_id" ... Read More

MongoDB query to check the existence of multiple fields

AmitDiwan
Updated on 11-May-2020 09:45:59

825 Views

To check the existence of multiple fields, use $exists along with $and. Let us create a collection with documents −> db.demo475.insertOne({"StudentFirstName":"Chris", "StudentAge":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c113b0f3fa88e2279088") } > db.demo475.insertOne({"StudentFirstName":"Bob", "StudentAge":21, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c127b0f3fa88e2279089") } > db.demo475.insertOne({"StudentFirstName":"David", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c135b0f3fa88e227908a") }Display all documents from a collection with the help of find() method −> db.demo475.find();This will produce the following output −{ "_id" : ObjectId("5e80c113b0f3fa88e2279088"), "StudentFirstName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5e80c127b0f3fa88e2279089"), "StudentFirstName" : "Bob", "StudentAge" : 21, "StudentCountryName" : "US" } { ... Read More

MongoDB function to return a specific data/value?

AmitDiwan
Updated on 11-May-2020 09:43:37

158 Views

To return a specific data, use findOne() in MongoDB. The findOne() method returns one document that satisfies the specified query criteria on the collection Let us create a collection with documents −> db.demo473.insertOne( ... { ...    "_id" : new ObjectId(), ...    "Name" : "Chris", ...    "details" : { ...       "X-Coordinate" :10, ...       "Y-Coordinate" :15 ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e805a07b0f3fa88e227907d") } > db.demo473.insertOne( ... { ...    "_id" : new ObjectId(), ...    "Name" : "Bob", ...    "details" : { ... Read More

Finding documents in MongoDB collection where a field is equal to given integer value?

AmitDiwan
Updated on 11-May-2020 09:43:08

213 Views

To find documents where a field is equal to given integer, use find(). Let us create a collection with documents −> db.demo472.insertOne({"Project_Id":-101, "ProjectName":"Online Customer Tracking"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80586cb0f3fa88e227907a") } > db.demo472.insertOne({"Project_Id":101, "ProjectName":"Online Banking System"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805884b0f3fa88e227907b") } > db.demo472.insertOne({"Project_Id":102, "ProjectName":"Online Library System"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805893b0f3fa88e227907c") }Display all documents from a collection with the help of find() method −> db.demo472.find();This will produce the following output −{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online Customer Tracking" } { "_id" : ObjectId("5e805884b0f3fa88e227907b"), "Project_Id" : 101, ... Read More

Advertisements