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 215 of 546
How do I remove elements not matching conditions in MongoDB?
To remove elements, use $pull and for such conditions, use $ne. The $ne in MongoDB is used to select the documents where the value of the field is not equal to the specified value.Let us create a collection with documents −> db.demo410.insertOne( ... { ... details: [{isMarried:false}, {isMarried:true}, {isMarried:false}, {isMarried:"Chris"}] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e70efc515dc524f70227681") }Display all documents from a collection with the help of find() method −> db.demo410.find();This will produce the following output −{ "_id" : ObjectId("5e70efc515dc524f70227681"), "details" : [ { "isMarried" : false }, { ...
Read MoreUnderstanding MongoDB Query Plan
To get a query plan in MongoDB, use explain(). The $explain operator gives information on the query plan.Let us create a collection with documents −> db.demo408.insertOne({"Value":50}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3a115dc524f70227678") } > db.demo408.insertOne({"Value":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3a715dc524f70227679") } > db.demo408.insertOne({"Value":45}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3ac15dc524f7022767a") } > db.demo408.insertOne({"Value":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e70e3af15dc524f7022767b") } > db.demo408.createIndex({Value:1}); { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }Display all documents from a collection with the help ...
Read MoreHow to use arrays as filters by querying subdocuments in MongoDB?
For this, use $setIsSubset in MongoDB. Let us create a collection with documents −> db.demo407.insertOne( ... { ... Name:"Chris", ... "details" : [ ... { ... id:100 ... }, ... { ... id:110 ... }, ... { ... id:130 ... } ... ] ... } ... ); ...
Read MoreHow to create MongoDB user on existing database with correct role?
To create a new user in MongoDB, use createUser(). Following is the query −> db.createUser( ... { ... user: "John", ... pwd: "123456", ... roles: [ { role: "readWrite", db: "test" } ], ... mechanisms: [ "SCRAM-SHA-256" ] ... } ... )This will produce the following output −Successfully added user: { "user" : "John", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] }Following is the query to show all users −> db.getUsers();This will produce the following output −[ { "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] } ]
Read MoreWorking with MongoDB find()
The find() in MongoDB selects documents in a collection or view and returns a cursor to the selected documents.The find() method with no parameters returns all documents from a collection and returns all fields for the documents. Let us see an example and create a collection with documents −> db.demo405.insertOne({"StudentInfo":{"Name":"Chris"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9134fac4d418a0178595") } > db.demo405.insertOne({"StudentInfo":{"Name":"David"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9138fac4d418a0178596") } > db.demo405.insertOne({"StudentInfo":{"Name":"Bob"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f913cfac4d418a0178597") } > db.demo405.insertOne({"StudentInfo":{"Name":"John"}}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f9140fac4d418a0178598") }Display all documents from a ...
Read MoreTransaction lock in MongoDB?
Transaction support isn’t available in MongoDB 4.0. To get similar results, use findOneAndUpdate().Let us create a collection with documents −> db.demo404.insertOne({"FirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c38fac4d418a0178592") } > db.demo404.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c3cfac4d418a0178593") } > db.demo404.insertOne({"FirstName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c40fac4d418a0178594") }Display all documents from a collection with the help of find() method −> db.demo404.find();This will produce the following output −{ "_id" : ObjectId("5e6f8c38fac4d418a0178592"), "FirstName" : "John" } { "_id" : ObjectId("5e6f8c3cfac4d418a0178593"), "FirstName" : "Robert" } { "_id" : ObjectId("5e6f8c40fac4d418a0178594"), "FirstName" : "Mike" }Following is the query ...
Read MoreMongoDB query to sort by words
To sort by words, use $addField along with $cond. Let us create a collection with documents −> db.demo62.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287084cfb11e5c34d8992f") } > db.demo62.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287085cfb11e5c34d89930") } > db.demo62.insertOne({"Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287086cfb11e5c34d89931") }Display all documents from a collection with the help of find() method −> db.demo62.find();This will produce the following output −{ "_id" : ObjectId("5e287084cfb11e5c34d8992f"), "Subject" : "MySQL" } { "_id" : ObjectId("5e287085cfb11e5c34d89930"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e287086cfb11e5c34d89931"), "Subject" : "Java" }Following is the query to sort by ...
Read MoreHow we can perform sort on ObjectId column in MongoDB?
To perform sort on ObjectId column, use sort(). Let us create a collection with document.> db.demo403.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b0fac4d418a017858e") } > db.demo403.insertOne({"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b2fac4d418a017858f") } > db.demo403.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b5fac4d418a0178590") } > db.demo403.insertOne({"Name":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f89b8fac4d418a0178591") }Display all documents from a collection with the help of find() method −> db.demo403.find();This will produce the following output −{ "_id" : ObjectId("5e6f89b0fac4d418a017858e"), "Name" : "Chris" } { "_id" : ObjectId("5e6f89b2fac4d418a017858f"), "Name" : "David" } { "_id" : ObjectId("5e6f89b5fac4d418a0178590"), ...
Read MoreHow to get the intersection of two arrays in MongoDB?
To get intersection of two arrays, use $setIntersection along with aggregate(). Let us create a collection with documents −> db.demo61.insertOne({"Values1":[10, 20, 30, 40, 50], "Values2":[30, 100, 70, 120, 40]}); { "acknowledged" : true, "insertedId" : ObjectId("5e286e28cfb11e5c34d8992a") }Display all documents from a collection with the help of find() method −> db.demo61.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e286e28cfb11e5c34d8992a"), "Values1" : [ 10, 20, 30, 40, 50 ], "Values2" : [ 30, 100, ...
Read MoreMongoDB query to get date records in a range
To get date records in a range, use $gt along with $lt. Let us create a collection with documents −> db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-11 12:30:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2863fecfb11e5c34d89927") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-10-12 03:10:00")}); { "acknowledged" : true, "insertedId" : ObjectId("5e28641acfb11e5c34d89928") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-14 05:11:20")}); { "acknowledged" : true, "insertedId" : ObjectId("5e28642acfb11e5c34d89929") }Display all documents from a collection with the help of find() method −> db.demo60.find();This will produce the following output −{ "_id" : ObjectId("5e2863fecfb11e5c34d89927"), "ArrivalDate" : ISODate("2019-01-11T12:30:10Z") } { "_id" : ObjectId("5e28641acfb11e5c34d89928"), "ArrivalDate" : ISODate("2019-10-12T03:10:00Z") } { "_id" : ObjectId("5e28642acfb11e5c34d89929"), "ArrivalDate" ...
Read More