Found 6705 Articles for Database

MongoDB query to concatenate values of array with other fields

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

1K+ 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

158 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

280 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

609 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

310 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

MongoDB query to find oldest date of three keys in each document

AmitDiwan
Updated on 02-Apr-2020 08:04:33

570 Views

To find the oldest date, use $min in MongoDB aggregate(). Let us create a collection with documents −> db.demo353.insertOne({"Date1":new ISODate("2019-01-10"), "Date2":new ISODate("2016-01-21"), "Date3":new ISODate("2020-04-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e568261f8647eb59e5620be") } > db.demo353.insertOne({"Date1":new ISODate("2011-11-20"), "Date2":new ISODate("2013-12-10"), "Date3":new ISODate("2014-01-05")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56827ff8647eb59e5620bf") }Display all documents from a collection with the help of find() method −> db.demo353.find();This will produce the following output −{ "_id" : ObjectId("5e568261f8647eb59e5620be"), "Date1" : ISODate("2019-01-10T00:00:00Z"), "Date2" : ISODate("2016-01-21T00:00:00Z"), "Date3" : ISODate("2020-04-11T00:00:00Z") } { "_id" : ObjectId("5e56827ff8647eb59e5620bf"), "Date1" : ISODate("2011-11-20T00:00:00Z"), "Date2" : ISODate("2013-12-10T00:00:00Z"), "Date3" : ISODate("2014-01-05T00:00:00Z") }Following is the query ... Read More

MongoDB query to update selected fields

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

490 Views

To update selected fields, use UPDATE() in MongoDB. The $set is used to set the new value. Let us create a collection with documents −> db.demo352.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55510af8647eb59e5620ba") } > db.demo352.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e55510ef8647eb59e5620bb") } > db.demo352.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e555112f8647eb59e5620bc") } > db.demo352.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e555115f8647eb59e5620bd") }Display all documents from a collection with the help of find() method −> db.demo352.find();This will produce the following output −{ "_id" : ObjectId("5e55510af8647eb59e5620ba"), "Name" : "Chris" } { "_id" ... Read More

How to find specific array elements in MongoDB document with query and filter with range?

AmitDiwan
Updated on 02-Apr-2020 08:00:44

206 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo351.insertOne( ... { ... ...    "_id" : "101", ...    "ProductDetails" : [ ...       { ...          "ProductName" : "Product-1", ...          "ProductPrice" :500 ...       }, ...       { ...          "ProductName" : "Product-2", ...          "ProductPrice" :400 ...       } ...    ] ... } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo351.insertOne( ... { ... ...   ... Read More

Query deeply nested Objects in MongoDB

AmitDiwan
Updated on 02-Apr-2020 07:56:23

2K+ Views

To query deeply nested objects, use dot(.) notation in MongoDB. Let us create a collection with documents −> db.demo350.insertOne( ... { ...    id:101, ...    Name: "Chris", ...    details: [ ...       { ...          _id: 1, ...          ClientNumber: "10001", ...          ClientDetails: [ . ...             { ...                Name:"David", ...                Age:29 ...             }, ...             ... Read More

Advertisements