Found 1659 Articles for Big Data Analytics

MongoDB slice array in populated field?

AmitDiwan
Updated on 13-May-2020 06:25:44

171 Views

To slice array, use a $slice operator in MongoDB. Let us create a collection with documents −> db.demo503.insertOne({_id:1, Name:"John", Subject:["MySQL", "Java", "C"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo503.insertOne({_id:2, Name:"David", Subject:["MongoDB", "C++", "Python"]}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo503.find().pretty();This will produce the following output −{ "_id" : 1, "Name" : "John", "Subject" : [ "MySQL", "Java", "C" ] } {    "_id" : 2,    "Name" : "David",    "Subject" : [       "MongoDB",       "C++",     ... Read More

MongoDB query to find on field combination of FirstName and LastName?

AmitDiwan
Updated on 13-May-2020 06:23:42

582 Views

For combination, use $concat and check the equality using $eq. Let us create a collection with documents −> db.demo502.insertOne({"FirstName":"John", "LastName":"Smith"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875534987b6e0e9d18f56d") } > db.demo502.insertOne({"FirstName":"David", "LastName":"Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e87553e987b6e0e9d18f56e") } > db.demo502.insertOne({"FirstName":"John", "LastName":"Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875543987b6e0e9d18f56f") }Display all documents from a collection with the help of find() method −> db.demo502.find();This will produce the following output −{ "_id" : ObjectId("5e875534987b6e0e9d18f56d"), "FirstName" : "John", "LastName" : "Smith" } { "_id" : ObjectId("5e87553e987b6e0e9d18f56e"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e875543987b6e0e9d18f56f"), "FirstName" : "John", ... Read More

MongoDB query to group duplicate documents

AmitDiwan
Updated on 13-May-2020 06:04:13

324 Views

To group duplicate documents, use MongoDB aggregate(). Let us create a collection with documents −> db.demo501.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f0987b6e0e9d18f566") } > db.demo501.insertOne({"Name":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f4987b6e0e9d18f567") } > db.demo501.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752f8987b6e0e9d18f568") } > db.demo501.insertOne({"Name":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752fb987b6e0e9d18f569") } > db.demo501.insertOne({"Name":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8752fd987b6e0e9d18f56a") } > db.demo501.insertOne({"Name":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875301987b6e0e9d18f56b") } > db.demo501.insertOne({"Name":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e875307987b6e0e9d18f56c") }Display all documents from a collection with the help of ... Read More

MongoDB query to select 10 most recent documents without changing order?

AmitDiwan
Updated on 13-May-2020 06:01:32

125 Views

For this, use skip() in MongoDB. Under skip(), set “count() – 10” to get 10 most recent documents. Let us create a collection with documents −> db.demo500.insertOne({value:10});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749c5987b6e0e9d18f55a") } > db.demo500.insertOne({value:1200});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749c8987b6e0e9d18f55b") } > db.demo500.insertOne({value:19});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749cb987b6e0e9d18f55c") } > db.demo500.insertOne({value:28});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749cf987b6e0e9d18f55d") } > db.demo500.insertOne({value:50});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749d1987b6e0e9d18f55e") } > db.demo500.insertOne({value:70});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8749d4987b6e0e9d18f55f") } > db.demo500.insertOne({value:100});{    "acknowledged" : true,    "insertedId" : ... Read More

Query array of subdocuments in MongoDB

AmitDiwan
Updated on 13-May-2020 06:11:52

392 Views

To query an array of subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo499.insertOne({ ... "details": ...    [ ...       { ...          Name :"MIT", ...          Rank: 1, ...          "CountryName":"US" ...       }, ... ...       { ...          Name :"Stanford University", ...          Rank: 2 ...       }, ... ...       { ...          Name :"University of California, Berkeley", ...       ... Read More

Retrieving group by result with arrays in MongoDB?

AmitDiwan
Updated on 13-May-2020 05:49:14

1K+ Views

To retrieve group by result with arrays, use aggregate(). We will also use $addToSet operator. It adds a value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo498.insertOne({id:1, Name:["Chris"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e86192b987b6e0e9d18f553") } > db.demo498.insertOne({id:2, Name:["David"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e86192d987b6e0e9d18f554") } > db.demo498.insertOne({id:3, Name:["Chris"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e861931987b6e0e9d18f555") } > db.demo498.insertOne({id:4, Name:["Bob"]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e861942987b6e0e9d18f556") } > db.demo498.insertOne({id:5, Name:["David"]});{    "acknowledged" : true,    "insertedId" ... Read More

Change date format in MongoDB

AmitDiwan
Updated on 13-May-2020 05:42:17

350 Views

We have the following date −01-10-2019To change the date format, let us use custom variable and convert date to a string and change its format −Following is the query to implement date to string −> var inputDate="01-10-2019"; > var formatDate= inputDate.split(/-|\//); > var outputString= formatDate[2]+'-'+formatDate[0]+'-'+formatDate[1];Displaying the variable value −> print(outputString);This will produce the following output −2019-01-10

How to find if element exists in document - MongoDB?

AmitDiwan
Updated on 13-May-2020 05:41:08

455 Views

To find if element exists in a MongoDB document, use MongoDB $exists. Let us create a collection with documents −> db.demo497.insertOne({"details":[{"Name":"Chris"}, {"Name":"Bob"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b3cfb0f3fa88e22790d1") } > db.demo497.insertOne({"details":[{"Name":"Carol"}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b3d9b0f3fa88e22790d2") } > db.demo497.insertOne({"details":[{}]});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b3e9b0f3fa88e22790d3") }Display all documents from a collection with the help of find() method −> db.demo497.find();This will produce the following output −{ "_id" : ObjectId("5e84b3cfb0f3fa88e22790d1"), "details" : [ { "Name" : "Chris" }, { "Name" : "Bob" } ] } { "_id" : ObjectId("5e84b3d9b0f3fa88e22790d2"), "details" : [ { "Name" : ... Read More

Display the undefined and exact MongoDB document records

AmitDiwan
Updated on 13-May-2020 05:38:59

280 Views

For this, use the forEach(). To display the values, use printjson(). Let us create a collection with documents −> db.demo496.insertOne({"Name":"David", "CountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce") } > db.demo496.insertOne({"Name":"John", "CountryName":"AUS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b054b0f3fa88e22790cf") } > db.demo496.insertOne({"Name":"Robert", "CountryName":"UK"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84b05db0f3fa88e22790d0") }Display all documents from a collection with the help of find() method −> db.demo496.find();This will produce the following output −{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" } { "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" } { "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : ... Read More

Update only a single MongoDB document without deleting any date

AmitDiwan
Updated on 13-May-2020 05:36:54

172 Views

To update only a single document, you need to update a specific data with updateOne(). The updateOne() is used to update a single document within the collection based on the filter.Let us create a collection with documents −> db.demo495.insertOne({"FirstName":"Chris", "Age":19});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84adfeb0f3fa88e22790ca") } > db.demo495.insertOne({"FirstName":"David", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae05b0f3fa88e22790cb") } > db.demo495.insertOne({"FirstName":"Bob", "Age":26});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae0eb0f3fa88e22790cc") } > db.demo495.insertOne({"FirstName":"John", "Age":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e84ae15b0f3fa88e22790cd") }Display all documents from a collection with the help of find() method −> db.demo495.find();This will produce ... Read More

Advertisements