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 241 of 546
Is there any way in MongoDB to get the inner value of json data?
To get inner value of JSON data, use find() along with dot(.) notation. Let us create a collection with documents −> db.demo235.insertOne( ... { ... "id":101, ... "details":[ ... { ... "Name":"Chris Brown", ... "Age":21 ... }, ... { ... "Name":"David Miller", ... "Age":24 ... } ... ], ... "otherdetails":[ ... ...
Read MoreUsing MongoDB Aggregate and GroupBy to get the frequency of name record
Let us first create a collection with documents −> db.demo232.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo232.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo232.insertOne({_id:103, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo232.insertOne({_id:104, Name:"David"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo232.insertOne({_id:105, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo232.find();This will produce the following output −{ "_id" : 101, "Name" : "Chris" } { "_id" : 102, "Name" : "Bob" } { "_id" : ...
Read MoreChange a unique index to a sparse unique index in MongoDB?
For sparse index, use sparse:true. Following is the query to create an index −> db.demo229.ensureIndex({"ClientName":1}, {unique: true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Following is the query to display indexes −> db.demo229.getIndexes();This will produce the following output −[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "test.demo229" }, { "v" : 2, "unique" ...
Read MoreFind document in MongoDB where at least one item from an array is not in the other?
For this, set regex in MongoDB find(). Let us create a collection with documents −> db.demo228.insertOne({"Subjects":["MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa51f03d395bdc213473b") } > db.demo228.insertOne({"Subjects":["MongoDB", "Java", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3fa52c03d395bdc213473c") }Display all documents from a collection with the help of find() method −> db.demo228.find();This will produce the following output −{ "_id" : ObjectId("5e3fa51f03d395bdc213473b"), "Subjects" : [ "MongoDB", "Java" ] } { "_id" : ObjectId("5e3fa52c03d395bdc213473c"), "Subjects" : [ "MongoDB", "Java", "MySQL" ] }Following is the query to find documents where at least one item from an array is not in the ...
Read MoreUpdating a set of documents from a list of key value pairs in MongoDB
Let us create a collection with documents −> db.demo227.insertOne({"_id":"101", "Name":"Chris"}); { "acknowledged" : true, "insertedId" : "101" } > db.demo227.insertOne({"_id":"102", "Name":"Bob"}); { "acknowledged" : true, "insertedId" : "102" }Display all documents from a collection with the help of find() method −> db.demo227.find();This will produce the following output −{ "_id" : "101", "Name" : "Chris" } { "_id" : "102", "Name" : "Bob" }Following is the query to update a set of documents from a list of key value pairs −> var bulkUpdateValue = [{"_id": "101", "Name": "Robert"}, ... {"_id": "102", "Name": "Sam"} ...]; > var bulkUpdate = db.demo227.initializeUnorderedBulkOp(); > ...
Read MoreMongoDB query to display all the values excluding the id?
For this, use the $project. The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fieldsLet us first create a collection with documents −> db.demo226.insertOne({"Name":"Chris", "Age":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3f9be803d395bdc2134738") } > db.demo226.insertOne({"Name":"Bob", "Age":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e3f9bf003d395bdc2134739") } > db.demo226.insertOne({"Name":"David", "Age":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3f9bf803d395bdc213473a") }Display all documents from a collection with the help of find() method −> db.demo226.find();This will produce the following ...
Read MoreHow to compare multiple properties in MongoDB?\\n
To compare multiple properties, use $where in MongoDB. Let us create a collection with documents −> db.demo223.insertOne({"Scores":[56, 78]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4ca03d395bdc2134730") } > db.demo223.insertOne({"Scores":[88, 45]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d103d395bdc2134731") } > db.demo223.insertOne({"Scores":[98, 79]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee4d803d395bdc2134732") }Display all documents from a collection with the help of find() method −> db.demo223.find();This will produce the following output −{ "_id" : ObjectId("5e3ee4ca03d395bdc2134730"), "Scores" : [ 56, 78 ] } { "_id" : ObjectId("5e3ee4d103d395bdc2134731"), "Scores" : [ 88, 45 ] } { "_id" : ObjectId("5e3ee4d803d395bdc2134732"), "Scores" : ...
Read MoreMongoDB query to add new array element in document
To add new array element in a MongoDB document, use $(projection) along with update(). Let us create a collection with documents −>db.demo222.insertOne({"details":[{"StudentName":"Chris", "StudentMarks":78}, {"StudentName":"David", "StudentMarks":89}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee31703d395bdc213472f") }Display all documents from a collection with the help of find() method −> db.demo222.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e3ee31703d395bdc213472f"), "details" : [ { "StudentName" : "Chris", "StudentMarks" : 78 }, { "StudentName" : "David", "StudentMarks" ...
Read MoreMongoDB regular expression to fetch record with specific name “John”, instead of “john”
For searching a specific word, use /searchWord/ with regex. Let us create a collection with documents −> db.demo221.insertOne({"Details":{"StudentName":"Chris", "StudentAge":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee15d03d395bdc213472b") } > db.demo221.insertOne({"Details":{"StudentName":"John", "StudentAge":20}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16503d395bdc213472c") } > db.demo221.insertOne({"Details":{"StudentName":"Bob", "StudentAge":22}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee16b03d395bdc213472d") } > db.demo221.insertOne({"Details":{"StudentName":"john", "StudentAge":24}}); { "acknowledged" : true, "insertedId" : ObjectId("5e3ee17303d395bdc213472e") }Display all documents from a collection with the help of find() method −> db.demo221.find();This will produce the following output −{ "_id" : ObjectId("5e3ee15d03d395bdc213472b"), "Details" : { "StudentName" : "Chris", "StudentAge" : 21 } ...
Read MoreGet all embedded documents with “isMarried” status in a MongoDB collection
To get all embedded documents, use $project in MongoDB. Let us create a collection with documents −> db.demo220.insertOne({ ... "id":101, ... "FullName" : "John Doe", ... "EmailId" : "john12@gmail.com", ... "ShippingDate" : new ISODate(), ... "details" : { "_id" :1001, "isMarried" :true } ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3eaf5b03d395bdc213471d") } > db.demo220.insertOne( ...{ ... "id":102, ... "FullName" : "John Smith", ... "EmailId" : "johnsmith@gmail.com", ... "ShippingDate" : new ISODate(), ... "details" : { "_id" :1002, "isMarried" :false } ... } ... ); { "acknowledged" ...
Read More