Found 6702 Articles for Database

MongoDB query check if value in array property?

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

2K+ Views

You can use $in operator to check if a value is in an array or not. Let us first create a collection with documents −> db.valueInArrayDemo.insertOne({"UserName":"John", "UserMessage":["Hi", "Hello", "Bye"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684cf7924bb85b3f48959") } > db.valueInArrayDemo.insertOne({"UserName":"Larry", "UserMessage":["Thank You", "Amazing", "Nice"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684d27924bb85b3f4895a") } >db.valueInArrayDemo.insertOne({"UserName":"Carol", "UserMessage":["Awesome", "Bye", "Cool"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd684d87924bb85b3f4895b") }Following is the query to display all documents from a collection with the help of find() method −> db.valueInArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd684cf7924bb85b3f48959"),    "UserName" : "John",    "UserMessage" : [       "Hi",       "Hello",     ... Read More

How do I push elements to an existing array in MongoDB?

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

292 Views

To push elements to an existing array, use $addToSet operator along with update(). Let us first create a collection with documents −> db.pushElements.insertOne({"Comments":["Good", "Awesome", "Nice"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd682597924bb85b3f48953") }Following is the query to display all documents from a collection with the help of find() method −> db.pushElements.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd682597924bb85b3f48953"),    "Comments" : [       "Good",       "Awesome",       "Nice"    ] }Following is the query to push elements to an existing array in MongoDB −> db.pushElements.update(    {_id:ObjectId("5cd682597924bb85b3f48953")},    { ... Read More

How to print document value in MongoDB shell?

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

1K+ Views

For this, work with the concept of forEach(). Let us first create a collection with documents −> db.printDocuementValueDemo.insertOne({"InstructorName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6804f7924bb85b3f48950") } > db.printDocuementValueDemo.insertOne({"InstructorName":"Sam Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd680577924bb85b3f48951") } > db.printDocuementValueDemo.insertOne({"InstructorName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd680637924bb85b3f48952") }Following is the query to display all documents from a collection with the help of find() method −> db.printDocuementValueDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6804f7924bb85b3f48950"),    "InstructorName" : "John Smith" } {    "_id" : ObjectId("5cd680577924bb85b3f48951"),    "InstructorName" : "Sam Williams" } ... Read More

How to remove white spaces (leading and trailing) from string value in MongoDB?

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

436 Views

For this, you need to write some code using forEach(). Let us first create a collection with documents −> db.removingWhiteSpaceDemo.insertOne({"Title":" Introduction to java "}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd66f387924bb85b3f4894c") }Following is the query to display all documents from a collection with the help of find() method −> db.removingWhiteSpaceDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd66f387924bb85b3f4894c"), "Title" : " Introduction to java " }Following is the query to remove white spaces (leading and trailing) from string value −> db.removingWhiteSpaceDemo.find({}, {"Title": 1 }).forEach(function(myDocument) {    myDocument.Title = myDocument.Title.trim();    db.removingWhiteSpaceDemo.update(       { "_id": myDocument._id ... Read More

How to add a field with static value to MongoDB find query?

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

467 Views

You can use $literal operator along with aggregate framework. Let us first create a collection with documents −> db.fieldWithStaticValue.insertOne({"Name":"Larry", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6554c7924bb85b3f48948") } > db.fieldWithStaticValue.insertOne({"Name":"Chris", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd655567924bb85b3f48949") } > db.fieldWithStaticValue.insertOne({"Name":"David", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd655607924bb85b3f4894a") }Following is the query to display all documents from a collection with the help of find() method −> db.fieldWithStaticValue.find();This will produce the following output −{ "_id" : ObjectId("5cd6554c7924bb85b3f48948"), "Name" : "Larry", "Age" : 24 } { "_id" : ObjectId("5cd655567924bb85b3f48949"), "Name" : "Chris", "Age" : 23 ... Read More

How to get tag count in MongoDB query results based on list of names?

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

107 Views

You can use $in operator. Let us first create a collection with documents −> db.tagCountDemo.insertOne({"ListOfNames":["John", "Sam", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b387924bb85b3f48944") } > db.tagCountDemo.insertOne({"ListOfNames":["Bob", "David", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b4b7924bb85b3f48945") } > db.tagCountDemo.insertOne({"ListOfNames":["Mike", "Robert", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b5d7924bb85b3f48946") } > db.tagCountDemo.insertOne({"ListOfNames":["James", "Carol", "Jace"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd64b717924bb85b3f48947") }Following is the query to display all documents from a collection with the help of find() method −> db.tagCountDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd64b387924bb85b3f48944"),    "ListOfNames" : ... Read More

MongoDB to sort by subdocument match?

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

253 Views

To sort by sub-document match, you can use aggregate framework. Let us first create a collection with documents −> db.sortBySubDocumentsDemo.insertOne(    {       "StudentName": "Chris",       "StudentDetails": [          {             "Age":21,             "StudentScore":91          },          {             "Age":22,             "StudentScore":99          },          {             "Age":21,             "StudentScore":93   ... Read More

How do you earn bitcoins?

Prasanna Kotamraju
Updated on 30-Jul-2019 22:30:26

118 Views

Bitcoin is the latest buzz in the world of digital currency at the moment and those who see its significance from a permissive outlook, they go for it because they know it that through this magical currency they can make money faster and become rich. Well, I am sorry to burst that bubble, but to be honest; Bitcoin is like any other currency out there. Like there is no easy way to make money, similarly, there is no other or magical way to gain cryptocurrency or Bitcoins too.However, cryptocurrency may open up a new method of earning, but the basic ... Read More

What is the Difference Between a Block chain and a Database?

Prasanna Kotamraju
Updated on 30-Jul-2019 22:30:26

126 Views

The difference between a Block chain and a traditional database begins with architecture, creation, access, and permissions. They differ in each and every aspect except that they both are huge repositories of data which is stored and accessed in an organised form, digitally.DatabaseThis runs on a client-server network, where there is a central repository of data that will be accessed by those nodes who have permission to access the data. The data of the database is maintained by administrators, and mostly nodes will have access to retrieve the data as per their requirement.Database, which is an electronic collection of data ... Read More

Get documents expired before today in MongoDB?

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

269 Views

You can use $lte operator along with Date() for this. Let us first create a collection with documents. Here, we have set the date 2019-05-11, which is the current date −> db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd563b17924bb85b3f4893b") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-01-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd563bf7924bb85b3f4893c") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-05-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd563ca7924bb85b3f4893d") } > db.getDocumentsExpiredDemo.insertOne({"ArrivalDate":new ISODate("2019-02-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd563e77924bb85b3f4893e") }Following is the query to display all documents from a collection with the help of find() method −> db.getDocumentsExpiredDemo.find().pretty();This ... Read More

Advertisements