Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to update a single field in a capped collection in MongoDB?
To update, you need to use update() for the field whose collection is set with capped: true. Let us create a collection with documents −> db.createCollection("Demo112", { capped : true, size : 14, max : 3 } ); { "ok" : 1 } > db.demo112.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef47a9fd5fd66da21447e") } > db.demo112.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef47e9fd5fd66da21447f") } > db.demo112.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2ef4919fd5fd66da214480") }Display all documents from a collection with the help of find() method −> db.demo112.find();This will produce the following output −{ "_id" : ...
Read MoreMongoDB Aggregation to slice array inside array
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 MoreHow to only get the data of the nested JSON object in MongoDB?
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 MoreHow to work with variables in MongoDB query
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 MoreInsert array where element does not exist else update it (with multiple conditions)?
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 MoreImplement $dateToString on array items with MongoDB
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 MoreMongoDB Aggregate JSON array field for the matching field of other collection?
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 MoreMongoDB query to implement nor query to fetch documents except a specific document
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 MoreHow to group nested fields in MongoDB aggregation with count value in array?
At first, let us create a collection with documents −> db.demo99.insertOne( ... { ... ... 'Details': ... { ... 'X': ... { ... 'Values': [10, 30, 50], ... 'Number':3, ... }, ... 'Y': ... { ... 'Values': [1000, 180], ... 'Number': 2, ... } ... } ...
Read MoreIncrement a property value of an element in array object with MongoDB
To increment a property value of an element, use update() in MongoDB and in that, work with #$inc to increment. Let us first create a collection with documents −> db.demo97.insertOne({ ... "Details": [ ... { ... "Name": "Chris", ... "Marks": 45 ... }, ... { ... "Name": "Bob", ... "Marks": 88 ... }y ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2d6d24b8903cdd865577af") }Display all ...
Read More