MongoDB Aggregation to Slice Array Inside Array

AmitDiwan
Updated on 30-Mar-2020 11:58:52

480 Views

For this, use aggregate() in MongoDB. In that, use $slice to slice array inside array. Let us create a collection with documents −> db.demo111.insertOne( ...    { ...       "_id" : 101, ...       "Name" : "Chris", ...       "Details" : [ ...          { ...             "_id" : 101, ...             "Score" : 78, ...             "Subjects" : [ ...                { ...             ... Read More

Get Data of Nested JSON Object in MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:52:05

956 Views

To get the data of the nested JSON object in MongoDB, use findOne(). Let us create a collection with documents −> db.demo109.insertOne( ...    { ...       "Name" : "Chris", ...       "Subjects" : [ ...          { ...             "Id" : "100", ...             "Name":"MySQL", ...             "InstructorDetails" : [ ...                { ...                   "Name" : "John" ...         ... Read More

Differences Between Compact Strings and Compressed Strings in Java 9

raja
Updated on 30-Mar-2020 11:50:08

260 Views

Compact Strings have introduced in Java 9 to replace Java 6's Compressed Strings. Its implementation uses byte[] array instead of char[] array and a new field coder has introduced to identify whether it is LATIN1 or UTF16 format while Compressed Strings have introduced in Java 6 that can be used byte[] array for one byte per character, and continued to use char[] array for two bytes per character, previously it can be turned on using -XX: + UseCompressedStrings.Unlike Compressed Strings, Compact Strings do not require un-packing or re-packing. Hence Compact String gives better performance at runtime.Compressed Strings are not enabled by default in Java 6, and need to be ... Read More

Remove Duplicate Entries by Two Keys in MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:47:54

813 Views

To remove duplicate entries by two keys, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo108.insertOne({"Value1":23, "Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3e49fd5fd66da214477") } > db.demo108.insertOne({"Value1":23, "Value2":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f29fd5fd66da214478") } > db.demo108.insertOne({"Value1":23, "Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f59fd5fd66da214479") }Display all documents from a collection with the help of find() method −> db.demo108.find();This will produce the following output −{ "_id" : ObjectId("5e2ee3e49fd5fd66da214477"), "Value1" : 23, "Value2" : 24 } { "_id" : ObjectId("5e2ee3f29fd5fd66da214478"), "Value1" : 23, "Value2" : 25 } { "_id" : ... Read More

Work with Variables in MongoDB Query

AmitDiwan
Updated on 30-Mar-2020 11:45:53

3K+ Views

To use variables, work with var in MongoDB. Let us create a collection with documents −> db.demo107.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b19fd5fd66da214471") } > db.demo107.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b49fd5fd66da214472") } > db.demo107.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee1b89fd5fd66da214473") }Display all documents from a collection with the help of find() method −> db.demo107.find();This will produce the following output −{ "_id" : ObjectId("5e2ee1b19fd5fd66da214471"), "Name" : "Chris" } { "_id" : ObjectId("5e2ee1b49fd5fd66da214472"), "Name" : "Bob" } { "_id" : ObjectId("5e2ee1b89fd5fd66da214473"), "Name" : "David" }Following is the query to use variable in ... Read More

Insert Array Where Element Does Not Exist or Update with Multiple Conditions

AmitDiwan
Updated on 30-Mar-2020 11:40:23

325 Views

You can use bulkWrite(). Let us create a collection with documents −> db.demo105.insertOne( { _id:'101', Name:'Chris', Details:[{ Marks1:60, Marks2:70, Marks3:70 }, { Marks1:70, Marks2:70, Marks3:90 }] } ); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() method −> db.demo105.find().pretty();This will produce the following output −{    "_id" : "101",    "Name" : "Chris",    "Details" : [       {          "Marks1" : 60,          "Marks2" : 70,          "Marks3" : 70       },       { ... Read More

Implement dateToString on Array Items with MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:33:48

290 Views

To implement $dateToString on array items, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo104.insertOne( ...    { ... ...       "AppName" : "Online Book", ...       "Details" : [ ...          { ...             "ClientName" : "Chris", ...             "Deadline" : new ISODate("2020-03-10") ...          } ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ed7fd9fd5fd66da21446f") }Display all documents from a collection with the help of ... Read More

Query on the Last Object of an Array with MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:30:20

879 Views

To query on the last object of an array, use aggregate(). Let us create a collection with documents −> db.demo103.insertOne( { "Details" : [    { "StudentId" : 101, "Details" : "MongoDB" },    {"StudentId" : 102, "Details" : "MySQL" },    { "StudentId" : 103, "Details" : "Java" } ], "Details1" : [ { "StudentId" : 104, "Number" : 3 } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ed2dd9fd5fd66da21446e") }Display all documents from a collection with the help of find() method −> db.demo103.find();This will produce the following output −{ "_id" : ObjectId("5e2ed2dd9fd5fd66da21446e"), "Details" : ... Read More

Aggregate JSON Array Field for Matching Field of Other Collection in MongoDB

AmitDiwan
Updated on 30-Mar-2020 11:27:49

624 Views

For this, create two collections and add some document. After that, use $lookup for match. Let us create a collection with documents −> db.demo101.insertOne( ... { "_id" : "1", "Details" : [ { "PId" : "200" }, { "PId" : "201" }, { "PId" : "201" } ] } ... ) { "acknowledged" : true, "insertedId" : "1" }Display all documents from a collection with the help of find() method −> db.demo101.find();This will produce the following output −{ "_id" : "1", "Details" : [ { "PId" : "200" }, { "PId" : "201" }, { "PId" : "201" } ] ... Read More

MongoDB Query to Implement NOR Query

AmitDiwan
Updated on 30-Mar-2020 11:25:31

194 Views

To fetch documents except a specific document, set the document to be missed using the $nor in MongoDB. Let us create a collection with documents −> db.demo100.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d9624b8903cdd865577c0") } > db.demo100.insertOne({"Name":"David", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d962cb8903cdd865577c1") } > db.demo100.insertOne({"Name":"Bob", "Age":19}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2d9634b8903cdd865577c2") }Display all documents from a collection with the help of find() method −> db.demo100.find();This will produce the following output −{ "_id" : ObjectId("5e2d9624b8903cdd865577c0"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e2d962cb8903cdd865577c1"), "Name" : "David", ... Read More

Advertisements