Found 6705 Articles for Database

Get distinct first word from a string with MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

222 Views

To get distinct first word from a string, you can use distinct(). Let us first create a collection with documents −> db.distinctFirstWordDemo.insertOne(    {       "_id": 100,       "StudentName":"John",       "StudentFeature": "John is a good player",       "Subject":"MongoDB"    } ); { "acknowledged" : true, "insertedId" : 100 } > db.distinctFirstWordDemo.insertOne(    {       "_id": 101,       "StudentName":"Carol",       "StudentFeature": "Carol is not a good player",       "Subject":"MongoDB"    } ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection ... Read More

How to query all items in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

326 Views

To query all items, use find(). Let us first create a collection with documents −> db.queryAllItemsDemo.insertOne({"StudentDetails":{"StudentName":"John", "StudentSubject":["MongoDB", "MySQL"], "StudentSubjectPrice":[4000, 6000]}, "OtherDetails":{"UserAge":29, "UserCountryName":"US"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef74ecef71edecf6a1f69f") }Display all documents from a collection with the help of find() method −> db.queryAllItemsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cef74ecef71edecf6a1f69f"),    "StudentDetails" : {       "StudentName" : "John",       "StudentSubject" : [          "MongoDB",          "MySQL"       ],       "StudentSubjectPrice" : [          4000,         ... Read More

Display a value with $addToSet in MongoDB with no duplicate elements?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

207 Views

Use $addToSet operator to ensures that there are no duplicate items added to the set. Let us first create a collection with documents −> db.getDistinctDemo.insertOne({"Values":[100, 200]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef69f9ef71edecf6a1f69d") } > db.getDistinctDemo.insertOne({"Values":[300, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef6a07ef71edecf6a1f69e") }Display all documents from a collection with the help of find() method −> db.getDistinctDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cef69f9ef71edecf6a1f69d"),    "Values" : [       100,       200    ] } {    "_id" : ObjectId("5cef6a07ef71edecf6a1f69e"),    "Values" : [       300, ... Read More

Extract particular element in MongoDB within a Nested Array?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

628 Views

To extract the particular element in MongoDB, you can use $elemMatch operator. Let us first create a collection with documents −> db.particularElementDemo.insertOne(    {       "GroupId" :"Group-1",       "UserDetails" : [          {             "UserName" : "John",             "UserOtherDetails" : [                {                   "UserEmailId" : "John123@gmail.com",                   "UserFriendName" : [                     ... Read More

How to dynamically build MongoDB query?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

2K+ Views

To build query dynamically, you need to write some script. Let us first create a collection with documents −> db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["MongoDB", "MySQL"]}); {     "acknowledged" : true,     "insertedId" : ObjectId("5cef5c5def71edecf6a1f69a") } > db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["C", "C++"]}); {     "acknowledged" : true,     "insertedId" : ObjectId("5cef5c73ef71edecf6a1f69b") } > db.dynamicQueryDemo.insertOne({"Name":"John", "Subject":["MongoDB", "Java"]}); {     "acknowledged" : true,     "insertedId" : ObjectId("5cef5c8bef71edecf6a1f69c") }Display all documents from a collection with the help of find() method −> db.dynamicQueryDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cef5c5def71edecf6a1f69a"),    "Name" : "John",    "Subject" : [       ... Read More

Retrieving an embedded object as a document via the aggregation framework in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

310 Views

To retrieve an embedded object as a document, use the aggregation $replaceRoot. Let us first create a collection with documents −> db.embeddedObjectDemo.insertOne(    { _id: new ObjectId(),       "UserDetails": { "UserName": "John", "UserAge": 24, "UserEmailId": "John22@gmail.com" }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ced580fef71edecf6a1f693") } > db.embeddedObjectDemo.insertOne( { _id: new ObjectId(), "UserDetails": { "UserName": "Carol", "UserAge": 26, "UserEmailId": "Carol123@gmail.com" } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ced5828ef71edecf6a1f694") }Following is the query to display all documents from a collection with the help of find() method −> db.embeddedObjectDemo.find().pretty();This will produce the ... Read More

Query array of nested string with MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

192 Views

To query array of nested string, you can use the dot(.) notation. Let us first create a collection with documents −> db.nestedStringDemo.insertOne(    {       "CustomerName": "John",       "CustomerOtherDetails": [ { "Age":29, "CountryName": "US" },       { "CompanyName": "Amazon",       "Salary": 150000, "ProjectName": ["Online Library Management System", "Pig Dice Game"]    } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea4629ef71edecf6a1f690") } > db.nestedStringDemo.insertOne( {    "CustomerName": "Chris",    "CustomerOtherDetails": [ { "Age":27, "CountryName": "AUS" },    { "CompanyName": "Google",       "Salary": 250000, "ProjectName": ["Chat Application", "Game ... Read More

Replace an array field value with MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

309 Views

You can use positional operator $. Let us first create a collection with documents −> db.replaceAnArrayFieldValueDemo.insertOne({"StudentTechnicalSubjects":["MySQL", "SQL Server", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea41e0ef71edecf6a1f68f") }Following is the query to display all documents from a collection with the help of find() method −> db.replaceAnArrayFieldValueDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cea41e0ef71edecf6a1f68f"),    "StudentTechnicalSubjects" : [       "MySQL",       "SQL Server",       "PL/SQL"    ] }Following is the query to replace an array field value. Here, we are updating “SQL Server” with “MongoDB” −> db.replaceAnArrayFieldValueDemo.update(    {"StudentTechnicalSubjects":"SQL Server"},   ... Read More

Is it possible to use MongoDB field value as pattern in $regex?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

191 Views

Yes, for this, use $indexOfCP operator along with aggregate framework. Let us first create a collection with documents −> db.patterDemo.insertOne(    {       "ClientName": "John", "ClientWebsiteName":"webbuziness.com/John/business"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea40acef71edecf6a1f68d") } > db.patterDemo.insertOne(    {       "ClientName": "Carol", "ClientWebsiteName":"solvecoding.com/business"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea40acef71edecf6a1f68e") }Following is the query to display all documents from a collection with the help of find() method −> db.patterDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cea40acef71edecf6a1f68d"),    "ClientName" : "John",    "ClientWebsiteName" : "abcd.com" ... Read More

Match multiple criteria inside an array with MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

302 Views

For this, use aggregate framework with the $elemMatch operator. Let us first create a collection with documents −> db.matchMultipleCriteriaDemo.insertOne({    "EmployeeDetails": [       {"EmployeeName": "Chris", "Salary": 45000, "Language":"Java"},       {"EmployeeName": "Robert", "Salary": 41000, "Language":"Python"}    ] }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea3bf0ef71edecf6a1f689") } > db.matchMultipleCriteriaDemo.insertOne({    "EmployeeDetails": [       {"EmployeeName": "David", "Salary": 55000, "Language":"C++"},       {"EmployeeName": "Bob", "Salary": 61000, "Language":"C"}    ] }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cea3bf1ef71edecf6a1f68a") }Following is the query to display all documents from a collection with the help of find() ... Read More

Advertisements