Found 6705 Articles for Database

Query in MongoDB to perform an operation similar to LIKE operation

AmitDiwan
Updated on 02-Apr-2020 12:23:37

175 Views

For similar operation, you can use / searchLetter /. Let us first create a collection with documents −> db.demo26.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c9dc22d07d3b95082e79") } > db.demo26.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c9e022d07d3b95082e7a") } > db.demo26.insertOne({"StudentName":"Jones"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14ca7222d07d3b95082e7b") } > db.demo26.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14ca7622d07d3b95082e7c") }Display all documents from a collection with the help of find() method −> db.demo26.find();This will produce the following output −{ "_id" : ObjectId("5e14c9dc22d07d3b95082e79"), "StudentName" : "Chris" } { "_id" : ObjectId("5e14c9e022d07d3b95082e7a"), "StudentName" : "John" } { ... Read More

Querying from part of object in an array with MongoDB

AmitDiwan
Updated on 02-Apr-2020 12:22:11

178 Views

To query from part of object in an array, use $findOne() and $all. Let us first create a collection with documents −> db.demo25.insertOne( ... { ... ...    "Details":[ ...       { ...          "UserId":"Carol101", ...          "UserName":"Carol" ...       }, ...       { ...          "UserId":"David102", ...          "UserName":"David" ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c86e22d07d3b95082e77") } > db.demo25.insertOne( ... { ... ...    "Details":[ ...   ... Read More

MongoDB query to push a computed expression in a $group?

AmitDiwan
Updated on 02-Apr-2020 12:19:13

271 Views

To push a computed expression in $group, use aggregate. Let us first create a collection with documents −> db.demo24.insertOne({"Id":100, "Status":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c58722d07d3b95082e72") } > db.demo24.insertOne({"Id":100, "Status":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c58a22d07d3b95082e73") } > db.demo24.insertOne({"Id":100, "Status":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c58f22d07d3b95082e74") } > db.demo24.insertOne({"Id":100, "Status":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c59122d07d3b95082e75") } > db.demo24.insertOne({"Id":100, "Status":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c59222d07d3b95082e76") }Display all documents from a collection with the help of find() method −> db.demo24.find();This will produce the following output ... Read More

Working with MongoDB $sort for sub array document

AmitDiwan
Updated on 02-Apr-2020 12:17:52

718 Views

For sub array document in MongoDB, use aggregate along with $sort. Let us first create a collection with documents −> db.demo23.insertOne( ...{ ... ...    "StudentDetails" : [{ ...       "Name" : "David", ...       "Age" : 23, ... ...    }, { ...          "Name" : "Adam", ...          "Age" : 24, ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c3eb22d07d3b95082e71") }Display all documents from a collection with the help of find() method −> db.demo23.find().pretty()This will produce the following ... Read More

Sorting field value (FirstName) for MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:37:20

361 Views

To sort values, use sort() in MongoDB. Let us first create a collection with documents −> db.demo365.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5b6d0ada61456dc936f") } > db.demo365.insertOne({"FirstName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bad0ada61456dc9370") } > db.demo365.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bed0ada61456dc9371") } > db.demo365.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5c0d0ada61456dc9372") }Display all documents from a collection with the help of find() method −> db.demo365.find();This will produce the following output −{ "_id" : ObjectId("5e57d5b6d0ada61456dc936f"), "FirstName" : "Chris" } { "_id" : ObjectId("5e57d5bad0ada61456dc9370"), "FirstName" : "Adam" } { "_id" : ... Read More

MongoDB query to find records with keys containing dots?

AmitDiwan
Updated on 02-Apr-2020 08:35:48

805 Views

For this, use $addFields. In that, use the $objectToArray to get the data in the form of keys and values. Use $filter with $indexOfBytes to check if there are any keys with. inside it.Let us first create a collection with documents −> db.demo364.insertOne( ...   { ...       "details" : { ...          "details1.otherdetails.Name" : {"FirstName":"Chris" } ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d485d0ada61456dc936d") } > db.demo364.insertOne( ...    { ...       "details" : { ...          "details1.otherdetails.Name" ... Read More

How to use deleteOne() function in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 08:31:01

281 Views

The deleteOne() function in MongoDB deletes at most one matching document from a collection. Let us first create a collection with documents −> db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c3d0ada61456dc9369") } > db.demo363.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c7d0ada61456dc936a") } > db.demo363.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2cad0ada61456dc936b") } > db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2d1d0ada61456dc936c") }Display all documents from a collection with the help of find() method −> db.demo363.find();This will produce the following output −{ "_id" : ObjectId("5e57d2c3d0ada61456dc9369"), "Name" : "Chris" } { "_id" : ObjectId("5e57d2c7d0ada61456dc936a"), ... Read More

Fetch multiple documents in MongoDB query with OR condition?

AmitDiwan
Updated on 02-Apr-2020 08:26:29

174 Views

Let us create a collection with documents −> db.demo362.insertOne({"ClientName":"John", "ClientProject":"School Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a77454a481fef8ec7a1c") } > db.demo362.insertOne({"ClientName":"David", "ClientProject":"Library Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a78454a481fef8ec7a1d") } > db.demo362.insertOne({"ClientName":"Mike", "ClientProject":"Event Tracker"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7a054a481fef8ec7a1e") } > db.demo362.insertOne({"ClientName":"Carol", "ClientProject":"Hospital Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7b754a481fef8ec7a1f") }Display all documents from a collection with the help of find() method −> db.demo362.find();This will produce the following output −{ "_id" : ObjectId("5e56a77454a481fef8ec7a1c"), "ClientName" : "John", "ClientProject" : "School Management System" } { "_id" : ... Read More

Using object as key for finding values in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:24:31

1K+ Views

For finding values, use find() in MongoDB. Under that, use dot notation. Let us create a collection with documents −> db.demo361.insertOne({"details":{"FirstName":"Chris", "LastName":"Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a61f54a481fef8ec7a19") } > db.demo361.insertOne({"details":{"FirstName":"David", "LastName":"Miller"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a62854a481fef8ec7a1a") } > db.demo361.insertOne({"details":{"FirstName":"John", "LastName":"Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a63654a481fef8ec7a1b") }Display all documents from a collection with the help of find() method −> db.demo361.find();This will produce the following output −{ "_id" : ObjectId("5e56a61f54a481fef8ec7a19"), "details" : { "FirstName" : "Chris", "LastName" : "Brown" } } { "_id" : ObjectId("5e56a62854a481fef8ec7a1a"), "details" : { "FirstName" : ... Read More

Find current and previous documents in MongoDB

AmitDiwan
Updated on 02-Apr-2020 08:22:14

658 Views

To get the current and previous documents, use sort(). Since we need only 2 documents, therefore, use LIMIT(2).Let us create a collection with documents −> db.demo360.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a10c54a481fef8ec7a15") } > db.demo360.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11254a481fef8ec7a16") } > db.demo360.insertOne({id:103, "Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11a54a481fef8ec7a17") } > db.demo360.insertOne({id:104, "Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11f54a481fef8ec7a18") }Display all documents from a collection with the help of find() method −> db.demo360.find();This will produce the following output −{ "_id" : ObjectId("5e56a10c54a481fef8ec7a15"), "id" : ... Read More

Advertisements