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 228 of 546
Check for existing documents/embedded documents in MongoDB
To check for existing documents/embedded documents, use $exists in MongoDB. Let us create a collection with documents −> db.demo322.insertOne( ... {'id':1001, ... 'details':[{'Score':10000, Name:"Bob"}, ... {'Score':98000, Name:"Sam"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113e2f8647eb59e56206c") } > db.demo322.insertOne( ... {'id':10002, ... 'details':[{'Score':9000}, ... {'Score':91000} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e5113faf8647eb59e56206d") }Display all documents from a collection with the help of find() method −> db.demo322.find();This will produce the following output −{ "_id" ...
Read MoreHow to store array values in MongoDB?
Let us first create a collection with documents wherein we are storing array values −>db.demo321.insertOne({"UserDetails":[{"UserId":101, "UserName":"Chris"}, {"UserId":102, "UserName":"Mike"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511248f8647eb59e56206a") } >db.demo321.insertOne({"UserDetails":[{"UserId":103, "UserName":"Bob"}, {"UserId":104, "UserName":"Sam"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e511259f8647eb59e56206b") }Display all documents from a collection with the help of find() method −> db.demo321.find();This will produce the following output −{ "_id" : ObjectId("5e511248f8647eb59e56206a"), "UserDetails" : [ { "UserId" : 101, "UserName" : "Chris" }, { "UserId" : 102, "UserName" : "Mike" } ] } { "_id" : ObjectId("5e511259f8647eb59e56206b"), "UserDetails" : [ { "UserId" : 103, "UserName" : "Bob" }, ...
Read MoreMongoDB query for capped sub-collection in an array
In MongoDB, you cannot use capped for sub-collection. However, use capped on the overall document. To display a specific number of values from an array, prefer $slice.Let us create a collection with documents −> db.demo319.insertOne({"Scores":[100, 345, 980, 890]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ecf6f8647eb59e562064") } > db.demo319.insertOne({"Scores":[903, 10004, 84575, 844]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ed01f8647eb59e562065") }Display all documents from a collection with the help of find() method −> db.demo319.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e50ecf6f8647eb59e562064"), "Scores" : [ 100, 345, ...
Read MoreMongoDB query to pull a specific value from a document
To pull a specific value, use UPDATE with $pull. Let us create a collection with documents −> db.demo318.insertOne({Subject:["MySQL", "MongoDB", "Java"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea6df8647eb59e562062") } > db.demo318.insertOne({Subject:["Spring", "Hibernate"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e50ea78f8647eb59e562063") }Display all documents from a collection with the help of find() method −> db.demo318.find();This will produce the following output −{ "_id" : ObjectId("5e50ea6df8647eb59e562062"), "Subject" : [ "MySQL", "MongoDB", "Java" ] } { "_id" : ObjectId("5e50ea78f8647eb59e562063"), "Subject" : [ "Spring", "Hibernate" ] }Following is the query to $pull a specific value “Hibernate” −> db.demo318.update({Subject:"Hibernate"}, {$pull:{"Subject":"Hibernate"}}); WriteResult({ "nMatched" : 1, ...
Read MoreHow to get values greater than a specific value from an embedded list in MongoDB?
To get values greater than a specific value, use $gt along with find(). Let us create a collection with documents −> db.demo317.insertOne( ... {'id':101, ... 'details':[{'Score':78, Name:"Chris"}, ... {'Score':88, Name:"David"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e50e69cf8647eb59e562060") } > db.demo317.insertOne( ... {'id':102, ... 'details':[{'Score':90, Name:"Chris"}, ... {'Score':91, Name:"David"} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e50e6adf8647eb59e562061") }Display all documents from a collection with the ...
Read MoreHow to restrict inserting an item with the same name in MongoDB?
For this, use ensureIndex() and set unique:true. Let us create a collection with documents. Here, when we try to inert duplicate item, then a duplicate key error occurs −> db.demo316.ensureIndex({"SubjectName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo316.insertOne({"SubjectName":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e378f8647eb59e56205d") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50e37df8647eb59e56205e") } > db.demo316.insertOne({"SubjectName":"MongoDB"}); 2020-02-22T13:47:05.186+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo316 index: SubjectName_1 dup key: { : "MongoDB" } : WriteError({ "index" : 0, ...
Read MoreUpdating nested document in MongoDB
To update the nested document, use $set. Let us create a collection with documents −> db.demo315.insertOne({ _id :101, ... details: [ ... {Name: 'Chris', subjects: [{id:1001, SubjectName:"MySQL"}]} ... ] ... } ...) { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo315.find().pretty();This will produce the following output −{ "_id" : 101, "details" : [ { "Name" : "Chris", "subjects" : [ { ...
Read MoreGet the number of open connections in MongoDB?
To get the number of open connections, use serverStatus() in MongoDB. Following is the query −> db.serverStatus();This will produce the following output −{ "host" : "DESKTOP-QN2RB3H", "version" : "4.0.5", "process" : "mongod", "pid" : NumberLong(10540), "uptime" : 74156, "uptimeMillis" : NumberLong(74156688), "uptimeEstimate" : NumberLong(74156), "localTime" : ISODate("2020-01-07T15:09:18.366Z"), "asserts" : { "regular" : 0, "warning" : 0, "msg" : 0, "user" : 1, "rollovers" : 0 }, "connections" : { "current" : ...
Read MoreMongoDB query to set a sub item in an array?
You can use positional $ operator. Let us first create a collection with documents −> db.demo22.insertOne( ... { ... ProductId:101, ... ... ProductDetails: ... [ ... { ... ProductFirstPrice: '35', ... ProductSecondPrice: '75' ... }, ... { ... ProductFirstPrice: '', ... ProductSecondPrice:'' ... }, ... { ...
Read MoreHow can I update child objects in MongoDB database?
To update child objects, use $set in MongoDB. Let us first create a collection with documents −>db.demo21.insertOne({"StudentId":"STU-101", "StudentDetails":{"StudentName":"Chris", "StudentAge":21}}); { "acknowledged" : true, "insertedId" : ObjectId("5e14be8922d07d3b95082e6f") }Display all documents from a collection with the help of find() method −> db.demo21.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e14be8922d07d3b95082e6f"), "StudentId" : "STU-101", "StudentDetails" : { "StudentName" : "Chris", "StudentAge" : 21 } }Following is the query to update child objects in MongoDB −> db.demo21.update({"StudentId":'STU-101'}, {$set:{'StudentDetails.StudentName':'Robert'}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents ...
Read More