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 by AmitDiwan
Page 594 of 839
MongoDB query to find value in array with multiple criteria (range)
To find value in an array within a range, use $gt and $lt. Let us create a collection with documents −> db.demo341.insertOne({ ... "Name": "Chris", ... "productDetails" : [ ... { ... "ProductPrice" : { ... "Price" : 800 ... } ... }, ... { ... "ProductPrice" : { ... "Price" : 400 ... } ... ...
Read MoreMongoDB findById returning a list of documents instead of a single result? How to get only a single document?
To get only a single result, use findOne() and fetch on the basis of id. Let us create a collection with documents −> db.demo340.insertOne({_id:1, "Name":"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 1 } > db.demo340.insertOne({_id:2, "Name":"David", Age:23}); { "acknowledged" : true, "insertedId" : 2 } > db.demo340.insertOne({_id:3, "Name":"Bob", Age:20}); { "acknowledged" : true, "insertedId" : 3 } > db.demo340.insertOne({_id:4, "Name":"Sam", Age:19}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo340.find();This will produce the following output −{ "_id" : 1, "Name" : "Chris", "Age" : 21 } { ...
Read MoreZip two arrays and create new array of object in a reshaped form with MongoDB
For this, use aggregate along with $zip. The zip is used to transpose an array. Let us create a collection with documents −> db.demo339.insertOne({Id:101, Score1:["98", "56"], Score2:[67, 89]}); { "acknowledged" : true, "insertedId" : ObjectId("5e529ee5f8647eb59e5620a2") }Display all documents from a collection with the help of find() method −> db.demo339.find();This will produce the following output −{ "_id" : ObjectId("5e529ee5f8647eb59e5620a2"), "Id" : 101, "Score1" : [ "98", "56" ], "Score2" : [ 67, 89 ] }Following is the query to zip two arrays with $zip and create a new array of object −> db.demo339.aggregate([ ... { ... ...
Read MoreWorking with MongoDB $concatArrays in $project on existing multi-array field
The $concatArrays is used to concatenate arrays to return the concatenated array.Let us create a collection with documents −> db.demo338.insertOne({"Name":"Chris", "Marks1":[ [56, 67, 45], [67, 89, 90, 91]]}); { "acknowledged" : true, "insertedId" : ObjectId("5e5299baf8647eb59e56209f") }Display all documents from a collection with the help of find() method −> db.demo338.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e5299baf8647eb59e56209f"), "Name" : "Chris", "Marks1" : [ [ 56, 67, 45 ], [ ...
Read MoreHow to calculate sum in MongoDB with aggregate()?
To get the sum, use $sum along with aggregate(). Let us create a collection with documents −> db.demo337.insertOne({"Amount":100}); { "acknowledged" : true, "insertedId" : ObjectId("5e5231e5f8647eb59e56209b") } > db.demo337.insertOne({"Amount":500}); { "acknowledged" : true, "insertedId" : ObjectId("5e5231e9f8647eb59e56209c") } > db.demo337.insertOne({"Amount":400}); { "acknowledged" : true, "insertedId" : ObjectId("5e5231ebf8647eb59e56209d") }Display all documents from a collection with the help of find() method −> db.demo337.find();This will produce the following output −{ "_id" : ObjectId("5e5231e5f8647eb59e56209b"), "Amount" : 100 } { "_id" : ObjectId("5e5231e9f8647eb59e56209c"), "Amount" : 500 } { "_id" : ObjectId("5e5231ebf8647eb59e56209d"), "Amount" : 400 }Following is the query to calculate sum ...
Read MoreHow to update only one property in MongoDB?
To update only one property, use $addToSet in MongoDB. Let us create a collection with documents −> db.demo336.insertOne({"Name":"Chris", "Score":[45, 67, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5e522cb1f8647eb59e562097") } > db.demo336.insertOne({"Name":"David", "Score":[89, 93, 47]}); { "acknowledged" : true, "insertedId" : ObjectId("5e522cb2f8647eb59e562098") }Display all documents from a collection with the help of find() method −> db.demo336.find();This will produce the following output −{ "_id" : ObjectId("5e522cb1f8647eb59e562097"), "Name" : "Chris", "Score" : [ 45, 67, 78 ] } { "_id" : ObjectId("5e522cb2f8647eb59e562098"), "Name" : "David", "Score" : [ 89, 93, 47 ] }Following is the query to update only ...
Read MoreMongoDB query to convert a string with commas to double
For such conversion, use aggregate(). Let us create a collection with documents −> db.demo335.insertOne({"Value":"45, 67, 78.0"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a1cf8647eb59e562091") } > db.demo335.insertOne({"Value":"17664, 76, 534.0"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a26f8647eb59e562092") } > db.demo335.insertOne({"Value":"8899, 322, 135, 875.50"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522a34f8647eb59e562093") } > db.demo335.insertOne({"Value":"1, 533.07"}); { "acknowledged" : true, "insertedId" : ObjectId("5e522ab9f8647eb59e562094") }Display all documents from a collection with the help of find() method −> db.demo335.find();This will produce the following output −{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "Value" : "45, 67, 78.0" } { "_id" : ObjectId("5e522a26f8647eb59e562092"), ...
Read MoreMongoDB query to add matched key to list after query?
For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo334.insertOne({ ... "Name": "Chris", ... "Age": 21, ... "details": [{ ... "Subject": "MySQL", ... "Score": 78 ... }, { ... "Subject": "MongoDB", ... "Score": 45 ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e52241bf8647eb59e562090") }Display all documents from a collection with the help of find() method −> db.demo334.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e52241bf8647eb59e562090"), "Name" : "Chris", ...
Read MoreCreate an index for text search in MongoDB
Let us create a collection with documents −> db.demo331.insertOne({"Words":"This is a MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c35f8647eb59e562089") } > db.demo331.insertOne({"Words":"THIS is a MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e521c36f8647eb59e56208a") }Display all documents from a collection with the help of find() method−> db.demo331.find();This will produce the following output −{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" } { "_id" : ObjectId("5e521c36f8647eb59e56208a"), "Words" : "THIS is a MongoDB" }Following is the query to create an index for text search −> db.demo331.createIndex( {Words: "text" } ); { "createdCollectionAutomatically" : false, "numIndexesBefore" : ...
Read MoreRestrict returned data in MongoDB to display only specific values from documents
To restrict returned data, use find(). The values 0 and 1 for fields would decide what all field values would be visible or hidden.Let us create a collection with documents −> db.demo330.insertOne({"Id":101, "Name":"Chris", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e52149ff8647eb59e562081") } > db.demo330.insertOne({"Id":102, "Name":"Sam", "Age":24}); { "acknowledged" : true, "insertedId" : ObjectId("5e5214aaf8647eb59e562082") } > db.demo330.insertOne({"Id":103, "Name":"David", "Age":28}); { "acknowledged" : true, "insertedId" : ObjectId("5e5214b3f8647eb59e562083") } > db.demo330.insertOne({"Id":104, "Name":"Bob", "Age":23}); . { "acknowledged" : true, "insertedId" : ObjectId("5e5214bdf8647eb59e562084") }Display all documents from a collection with the help of find() method −> db.demo330.find();This ...
Read More