Found 1659 Articles for Big Data Analytics

How to use deleteOne() function in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:31:01

207 Views

The deleteOne() function in MongoDB deletes at most one matching document from a collection. Let us first create a collection with documents −> db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c3d0ada61456dc9369") } > db.demo363.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c7d0ada61456dc936a") } > db.demo363.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2cad0ada61456dc936b") } > db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2d1d0ada61456dc936c") }Display all documents from a collection with the help of find() method −> db.demo363.find();This will produce the following output −{ "_id" : ObjectId("5e57d2c3d0ada61456dc9369"), "Name" : "Chris" } { "_id" : ObjectId("5e57d2c7d0ada61456dc936a"), ... Read More

Fetch multiple documents in MongoDB query with OR condition?

AmitDiwan
Updated on 02-Apr-2020 08:26:29

91 Views

Let us create a collection with documents −> db.demo362.insertOne({"ClientName":"John", "ClientProject":"School Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a77454a481fef8ec7a1c") } > db.demo362.insertOne({"ClientName":"David", "ClientProject":"Library Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a78454a481fef8ec7a1d") } > db.demo362.insertOne({"ClientName":"Mike", "ClientProject":"Event Tracker"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7a054a481fef8ec7a1e") } > db.demo362.insertOne({"ClientName":"Carol", "ClientProject":"Hospital Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7b754a481fef8ec7a1f") }Display all documents from a collection with the help of find() method −> db.demo362.find();This will produce the following output −{ "_id" : ObjectId("5e56a77454a481fef8ec7a1c"), "ClientName" : "John", "ClientProject" : "School Management System" } { "_id" : ... Read More

Using object as key for finding values in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:24:31

1K+ Views

For finding values, use find() in MongoDB. Under that, use dot notation. Let us create a collection with documents −> db.demo361.insertOne({"details":{"FirstName":"Chris", "LastName":"Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a61f54a481fef8ec7a19") } > db.demo361.insertOne({"details":{"FirstName":"David", "LastName":"Miller"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a62854a481fef8ec7a1a") } > db.demo361.insertOne({"details":{"FirstName":"John", "LastName":"Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a63654a481fef8ec7a1b") }Display all documents from a collection with the help of find() method −> db.demo361.find();This will produce the following output −{ "_id" : ObjectId("5e56a61f54a481fef8ec7a19"), "details" : { "FirstName" : "Chris", "LastName" : "Brown" } } { "_id" : ObjectId("5e56a62854a481fef8ec7a1a"), "details" : { "FirstName" : ... Read More

Find current and previous documents in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:22:14

563 Views

To get the current and previous documents, use sort(). Since we need only 2 documents, therefore, use LIMIT(2).Let us create a collection with documents −> db.demo360.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a10c54a481fef8ec7a15") } > db.demo360.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11254a481fef8ec7a16") } > db.demo360.insertOne({id:103, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11a54a481fef8ec7a17") } > db.demo360.insertOne({id:104, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11f54a481fef8ec7a18") }Display all documents from a collection with the help of find() method −> db.demo360.find();This will produce the following output −{ "_id" : ObjectId("5e56a10c54a481fef8ec7a15"), "id" : ... Read More

MongoDB query to concatenate values of array with other fields

AmitDiwan
Updated on 02-Apr-2020 08:20:59

915 Views

To concatenate in MongoDB, use $concat in $project. Let us create a collection with documents −> db.demo359.insertOne( ...    { ... ...       Name1: "Chris", ...       Name2: "David", ...       Subjects: ["MySQL", "MongoDB", "Java"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5694cdf8647eb59e5620d0") }Display all documents from a collection with the help of find() method −> db.demo359.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e5694cdf8647eb59e5620d0"),    "Name1" : "Chris",    "Name2" : "David",    "Subjects" : [       "MySQL",       "MongoDB",   ... Read More

Does aggregation query with $match works in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:18:18

98 Views

Yes, it works in MongoDB. Let us create a collection with documents −> db.demo358.insertOne({"ClientId":101, "ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692c0f8647eb59e5620cb") } > db.demo358.insertOne({"ClientId":102, "ClientName":"David", "ClientAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692caf8647eb59e5620cc") } > db.demo358.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692d2f8647eb59e5620cd") } > db.demo358.insertOne({"ClientId":104, "ClientName":"Bob", "ClientAge":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692ddf8647eb59e5620ce") } > db.demo358.insertOne({"ClientId":105, "ClientName":"Chris", "ClientAge":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692e7f8647eb59e5620cf") }Display all documents from a collection with the help of find() method −> db.demo358.find();This will produce the following output ... Read More

MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?

AmitDiwan
Updated on 02-Apr-2020 08:15:55

214 Views

The NumberInt() is used to explicitly specify 32-bit integers. Let us create a collection with documents −> db.demo357.insertOne( ...    { ...       "FirstName" : "Chris", ...       "Age" : 21, ...       "details" : { ...          "studentDetails" : { ...             "id" : NumberInt(101) ...          } ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568fa6f8647eb59e5620c9") } > db.demo357.insertOne( ...    { ...       "FirstName" : "David", ...   ... Read More

How to validate documents before insert or update in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:13:38

522 Views

To validate documents, use the concept of validation. Following is the query −> db.createCollection("demo356", {validator: { ... $and: [ {"FirstName": {$type: "string", $exists: true}} ] ... }}) { "ok" : 1 }Let us create a collection with documents −> db.demo356.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568d49f8647eb59e5620c7") } > db.demo356.insertOne({"FirstName":909}); 2020-02-26T20:52:58.497+0530 E QUERY [js] WriteError: Document failed validation : WriteError({    "index" : 0,    "code" : 121,    "errmsg" : "Document failed validation",    "op" : {       "_id" : ObjectId("5e568d52f8647eb59e5620c8"),       "FirstName" : 909    } }) WriteError@src/mongo/shell/bulk_api.js:461:48 Bulk/mergeBatchResults@src/mongo/shell/bulk_api.js:841:49 Bulk/executeBatch@src/mongo/shell/bulk_api.js:906:13 Bulk/this.execute@src/mongo/shell/bulk_api.js:1150:21 DBCollection.prototype.insertOne@src/mongo/shell/crud_api.js:252:9 ... Read More

How to project specific elements in an array field with MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:10:30

3K+ Views

To project-specific elements in an array field, use $project. Let us create a collection with documents −>db.demo355.insertOne({"id":101, "details":[{"Name":"Chris", isMarried:1}, {"Name":"David", isMarried:0}, {"Name":"Mike", isMarried:1}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568928f8647eb59e5620c5") }Display all documents from a collection with the help of find() method −> db.demo355.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e568928f8647eb59e5620c5"),    "id" : 101,    "details" : [       {          "Name" : "Chris",          "isMarried" : 1       },       {          "Name" : "David",       ... Read More

MongoDB query for counting number of unique fields grouped by another field?

AmitDiwan
Updated on 02-Apr-2020 08:07:37

242 Views

For this, use aggregate() and group with $group. Let us create a collection with documents −> db.demo354.insertOne({"Name1":"Chris", "Name2":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685a6f8647eb59e5620c0") } > db.demo354.insertOne({"Name1":"Chris", "Name2":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685a9f8647eb59e5620c1") } > db.demo354.insertOne({"Name1":"Chris", "Name2":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685aff8647eb59e5620c2") } > db.demo354.insertOne({"Name1":"Carol", "Name2":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685c4f8647eb59e5620c3") } > db.demo354.insertOne({"Name1":"Carol", "Name2":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5685c5f8647eb59e5620c4") }Display all documents from a collection with the help of find() method −> db.demo354.find();This will produce the following output −{ "_id" ... Read More

Advertisements