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
Database Articles
Page 194 of 546
MongoDB collection query to exclude some fields in find()?
Set the fields you don’t want to include as 0 as in the below syntax. Here, we have set fields “yourFieldName1” and “yourFieldName2” as 0 −db.yourCollectionName.find(yourQuery, {yourFieldName1:0, yourFieldName2:0});To understand the above syntax, let us create a collection with documents −> db.demo567.insertOne({"Name":"Chris", Age:21});{ "acknowledged" : true, "insertedId" : ObjectId("5e908fa139cfeaaf0b97b57b") } > db.demo567.insertOne({"Name":"David", Age:23});{ "acknowledged" : true, "insertedId" : ObjectId("5e908fa939cfeaaf0b97b57c") } > db.demo567.insertOne({"Name":"Bob", Age:22});{ "acknowledged" : true, "insertedId" : ObjectId("5e908faf39cfeaaf0b97b57d") } > db.demo567.insertOne({"Name":"Carol", Age:20});{ "acknowledged" : true, "insertedId" : ObjectId("5e908fb539cfeaaf0b97b57e") }Display all documents from a collection with the help of find() method −> db.demo567.find();This will produce the following ...
Read MoreGrouping the array items in MongoDB and get the count the products with similar price?
To group the array items, use $group along with $sort. Let us create a collection with documents −> db.demo566.insertOne( ... { ... ... "ProductInformation" : [ ... { ... "ProductName" : "Product-1", ... "ProductPrice" :100 ... }, ... { ... "ProductName" : "Product-2", ... "ProductPrice" :1100 ... }, ... { ... "ProductName" : "Product-3", ... "ProductPrice" :100 ... ...
Read MoreMongoDB - how can I access fields in a document?
To access fields in a document, simply use find(). Let us create a collection with documents −> db.demo565.insertOne( ... { ... id:101, ... Name:"David", ... "CountryName":"US" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896739cfeaaf0b97b577") } > > db.demo565.insertOne( ... { ... id:102, ... Name:"Carol", ... "CountryName":"UK" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896839cfeaaf0b97b578") } > > db.demo565.insertOne( ... { ... id:103, ... Name:"Sam", ... "CountryName":"AUS" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e90896839cfeaaf0b97b579") }Display all ...
Read MoreMake MongoDB replace single array value with string?
To replace, use $set and positional($) operator. Let us create a collection with documents −> db.demo564.insertOne({"StudentName":["Chris", "David", "Mike", "Sam"]});{ "acknowledged" : true, "insertedId" : ObjectId("5e90880a39cfeaaf0b97b576") }Display all documents from a collection with the help of find() method −> db.demo564.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e90880a39cfeaaf0b97b576"), "StudentName" : [ "Chris", "David", "Mike", "Sam" ] }Following is the query to replace single array value with string −> db.demo564.updateMany( ... { "StudentName": "David" }, ... { "$set": { "StudentName.$": "Carol Taylor" } ...
Read MoreHow to get items with a specific value from documents using MongoDB shell?
To get items with a specific value, simply use find(). Let us create a collection with documents −> db.demo563.insertOne({"Name":"Chris", "Age":21, "isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f546c54b4472ed3e8e878") } > db.demo563.insertOne({"Name":"Bob", "Age":23, "isMarried":false}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f547854b4472ed3e8e879") } > db.demo563.insertOne({"Name":"Carol", "Age":23, "isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f548b54b4472ed3e8e87a") } > db.demo563.insertOne({"Name":"Mike", "Age":21, "isMarried":true}){ "acknowledged" : true, "insertedId" : ObjectId("5e8f549454b4472ed3e8e87b") }Display all documents from a collection with the help of find() method −> db.demo563.find();This will produce the following output −{ "_id" : ObjectId("5e8f546c54b4472ed3e8e878"), "Name" : "Chris", "Age" : 21, "isMarried" : true } { "_id" : ...
Read MoreHow to search for a record (field) and then delete it in MongoDB?
To search for a field, use $exists and to delete it, use $unset. The $unset operator in MongoDB deletes a particular field.Let us create a collection with documents −> db.demo562.insertOne({"Name":"Chris", "Age":21});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4ae854b4472ed3e8e872") } > db.demo562.insertOne({"Age":20});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4ae954b4472ed3e8e873") } > db.demo562.insertOne({"Name":"David", "Age":23});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f4aea54b4472ed3e8e874") }Display all documents from a collection with the help of find() method −> db.demo562.find();This will produce the following output −{ "_id" : ObjectId("5e8f4ae854b4472ed3e8e872"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8f4ae954b4472ed3e8e873"), "Age" : 20 } ...
Read MoreRandomizing unique data with MongoDB and placing values for emailid with word\\nJohn in the beginning
To randomize unique data, use Math.random() in MongoDB. Let us create a collection with documents −> db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490454b4472ed3e8e86c") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490654b4472ed3e8e86d") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490a54b4472ed3e8e86e") }Display all documents from a collection with the help of find() method −> db.demo561.find();This will produce the following output −{ "_id" : ObjectId("5e8f490454b4472ed3e8e86c"), "EmailId" : null } { "_id" : ObjectId("5e8f490654b4472ed3e8e86d"), "EmailId" : null } { "_id" : ObjectId("5e8f490a54b4472ed3e8e86e"), "EmailId" : null }Following is the query for randomizing unique data with MongoDB −> db.demo561.find().forEach(function(doc){ ... ...
Read MoreFetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
To match, use $match in MongoDB and to get data between two dates, use $gte and $lte. Let us create a collection with documents −> db.demo560.insertOne({"value1":40, "value2":40, shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e867") } > db.demo560.insertOne({"value1":20, "value2":60, shippingDate:new ISODate("2020-02-26")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e868") } > db.demo560.insertOne({"value1":40, "value2":70, shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e869") } > db.demo560.insertOne({"value1":40, "value2":130, shippingDate:new ISODate("2020-03-31")});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f3d5254b4472ed3e8e86a") }Display all documents from a collection with the help of find() method −> db.demo560.find();This will produce the following output −{ "_id" : ObjectId("5e8f3d5254b4472ed3e8e867"), "value1" : ...
Read MoreGROUP BY array of document to get the count of repeated Age values
To GROUP BY array of the document, use $group. Let us create a collection with documents −>db.demo559.insertOne({details:[{Name:"Chris", Age:21}, {Name:"Bob", Age:22}, {Name:"Carol", Age:21}, {Name:"Sam", Age:21}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e8f38d954b4472ed3e8e866") }Display all documents from a collection with the help of find() method −> db.demo559.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e8f38d954b4472ed3e8e866"), "details" : [ { "Name" : "Chris", "Age" : 21 }, { "Name" : "Bob", "Age" : ...
Read MoreMongoDB aggregate to get the count of field values of corresponding duplicate names?
Let us see an example and create a collection with documents −> db.demo558.insertOne( ... { ... _id : 100, ... CountryCode:101, ... details: [ ... { ... Name:"Chris", ... Subject:"MySQL" ... }, ... { ... Name:"Chris", ... Subject:"MongoDB" ... }, ... { ... Name:"Chris", ... Subject:"Java" ... }, ... { ... ...
Read More