Found 1349 Articles for MongoDB

Get substring in MongoDB aggregate

AmitDiwan
Updated on 27-Mar-2020 07:27:23

502 Views

To get substring, use $substr in MongoDB. Let us create a collection with documents −> db.demo176.insertOne({"ProductName":"PRODUCT-1"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a09e4f06af551997ef") } > db.demo176.insertOne({"ProductName":"PRODUCT-102"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843a69e4f06af551997f0") } > db.demo176.insertOne({"ProductName":"PRODUCT-105"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3843aa9e4f06af551997f1") }Display all documents from a collection with the help of find() method −> db.demo176.find();This will produce the following output −{ "_id" : ObjectId("5e3843a09e4f06af551997ef"), "ProductName" : "PRODUCT-1" } { "_id" : ObjectId("5e3843a69e4f06af551997f0"), "ProductName" : "PRODUCT-102" } { "_id" : ObjectId("5e3843aa9e4f06af551997f1"), "ProductName" : "PRODUCT-105" }Following is the query to get substring in MongoDB ... Read More

How do I use MongoDB to count only collections that match two fields?

AmitDiwan
Updated on 27-Mar-2020 06:52:51

78 Views

To count only collections that match two fields, use count(). Let us create a collection with documents −> db.demo175.insertOne({"EmployeeName":"Bob", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840969e4f06af551997e8") } > db.demo175.insertOne({"EmployeeName":"David", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e38409e9e4f06af551997e9") } > db.demo175.insertOne({"EmployeeName":"Mike", "isMarried":"YES"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840a79e4f06af551997ea") } > db.demo175.insertOne({"EmployeeName":"Sam", "isMarried":"NO"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3840ae9e4f06af551997eb") }Display all documents from a collection with the help of find() method −> db.demo175.find();This will produce the following output −{ "_id" : ObjectId("5e3840969e4f06af551997e8"), "EmployeeName" : "Bob", "isMarried" : "YES" } { "_id" ... Read More

Evaluate one of more values from a MongoDB collection with documents

AmitDiwan
Updated on 27-Mar-2020 06:51:19

75 Views

To evaluate one or more values, use $or along with find(). Let us create a collection with documents −> db.demo174.insertOne({"StudentName":"Chris", "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c709e4f06af551997e5") } > db.demo174.insertOne({"StudentName":"David", "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c779e4f06af551997e6") } > db.demo174.insertOne({"StudentName":"Bob", "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383c7e9e4f06af551997e7") }Display all documents from a collection with the help of find() method −> db.demo174.find();This will produce the following output −{ "_id" : ObjectId("5e383c709e4f06af551997e5"), "StudentName" : "Chris", "CountryName" : "US" } { "_id" : ObjectId("5e383c779e4f06af551997e6"), "StudentName" : "David", "CountryName" : "UK" } { "_id" : ... Read More

Limit number of values in a field with MongoDB?

AmitDiwan
Updated on 27-Mar-2020 06:49:39

127 Views

To limit the number of values in a field, use MongoDB $slice. Let us create a collection with documents −> db.demo173.insertOne({"ListOfValues":[10, 40, 100, 560, 700, 900]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e383a4f9e4f06af551997e4") }Display all documents from a collection with the help of find() method −> db.demo173.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e383a4f9e4f06af551997e4"),    "ListOfValues" : [10, 40, 100, 560, 700, 900 ] }Following is the query to limit number of values in a field using MongoDB −> db.demo173.find({}, { "ListOfValues": { "$slice": -2 } } );This will produce the following output −{ "_id" ... Read More

Find a value in lowercase from a MongoDB collection with documents

AmitDiwan
Updated on 27-Mar-2020 06:45:06

3K+ Views

To find a value in lowercase, use the toLowerCase() method in MongoDB. Use the method in find() to find the value in lowercase.Let us create a collection with documents −> db.demo172.insertOne({"SubjectName":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3838ce9e4f06af551997e1") } > db.demo172.insertOne({"SubjectName":"mongodb"}); {     "acknowledged" : true,    "insertedId" : ObjectId("5e3838d69e4f06af551997e2") } > db.demo172.insertOne({"SubjectName":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3838db9e4f06af551997e3") }Display all documents from a collection with the help of find() method −> db.demo172.find();This will produce the following output −{ "_id" : ObjectId("5e3838ce9e4f06af551997e1"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5e3838d69e4f06af551997e2"), "SubjectName" : "mongodb" } ... Read More

How to re-map the fields of a MongoDB collection?

AmitDiwan
Updated on 27-Mar-2020 06:40:03

208 Views

To re-map the fields of a MongoDB collection, use update() along with $rename. Let us first create a collection with documents −> db.demo171.insertOne( { "Name": "Chris", "Details": { "SubjectName": "MySQL", "CountryName": "US" } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3837 399e4f06af551997e0") }Display all documents from a collection with the help of find() method −> db.demo171.find();This will produce the following output −{ "_id" : ObjectId("5e3837399e4f06af551997e0"), "Name" : "Chris", "Details" : { "SubjectName" : "MySQL", "CountryName" : "US" } }Following is the query to re-map the fields of a MongoDB collection −> db.demo171.update({}, { $rename : { 'Name' ... Read More

How to Secure MongoDB on Ubuntu 16.04

Sharon Christine
Updated on 23-Jan-2020 12:33:23

206 Views

In this article, we will learn how to secure Mongo DB on Ubuntu 16.04. In previous versions the Mongo DB is vulnerable to automated exploits because, by default, there is no authentication which was allowed to interact with the database, any user can create, read, modify and destroy the database and the contents, this is because of the Mongo DB daemon which can listen to all interfaces as default settings.Enabling Authentication and Adding Admin UserThis issue has been mitigated in the latest versions of Mongo DB after version 3.x releases, however, the authentication is still disabled as default settings, so ... Read More

How to Install MongoDB on Ubuntu 16.04

Sharon Christine
Updated on 22-Jan-2020 06:45:35

451 Views

MongoDB is a cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document. The official Ubuntu 16.04 MongoDB packages have not yet been published by the MongoDB maintainers. This article explains about “How to install Mongo DB on Ubuntu and start the MongoDB service on boot”Adding the MongoDB RepositoryMongoDB is generally included in a Ubuntu package repositories. However, a legitimate MongoDB repository affords the most upto date version change in an endorsed manner.To carry out this process, we first must import the key for the legitimate MongoDB repository using ... Read More

How To Install and Configure MongoDB on CentOS 7

Sharon Christine
Updated on 20-Jan-2020 09:57:59

344 Views

In this article, we will learn how to install and configure the MongoDB on CentOS 7, MongoDB which is an open-source and free database (is a NoSQL database), means it is a document-oriented database, it stores the document which is similar structurally to JSON (in MongoDB it is called as BSON) with high availability, performance and auto scaling. Unlike the RDBMS, it doesn’t need any predefined database schema for adding the data to the database tables. We can alter the schema any point of time without disturbing the existing schema.PrerequisitesCentos 7 installed on the Linux Machine.A user with root user ... Read More

Difference between RDBMS and MongoDB

Mahesh Parahar
Updated on 28-Nov-2019 10:14:44

5K+ Views

RDBMSRDBMS stands for Relational Database Management System. It stores data in form of entity as tables. It provides multiple layers on information security. Each table may or may not have a primary key which identifies a record uniquely in a table and a foreign key which indentifies the relationship between two tables. RDBMS uses SQL language to query databases. Examples of popular RDBMS are oracle, sql server, mysql etc.MongoDBMongoDB is a NoSQL database. It is open source. It is a document oriented database and it uses BSON which is binary version of JSON. BSON is a document storage format. MongoDB ... Read More

Advertisements