Found 6702 Articles for Database

Fetch records in MongoDB on querying its subset

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

104 Views

You can use $all operator. Let us first create a collection with documents −> db.subsetOfAnArrayDemo.insertOne({"StudentProgrammingSkills":    ["Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9d1e1895c4fd159f80804") }Following is the query to display all documents from the collection with the help of find() method −> db.subsetOfAnArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9d1e1895c4fd159f80804"),    "StudentProgrammingSkills" : [       "Java",       "MongoDB",       "MySQL",       "C++",       "Data Structure",       "Algorithm",       "Python",     ... Read More

Extract subarray value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

361 Views

To extract the subarray value in MongoDB, you can use $elemMatch projection operator.Let us first create a collection with documents −> db.extractSubArrayDemo.insertOne( ...    { ...       _id: 101, ...       "clientName":"Larry", ...       "ClientDetails": ...       [ ...          { ...             "ClientProjectName":"Online Game", ...             "DeveloperTeamSize": 10 ...          }, ...          { ...             "ClientProjectName":"Pig Dice Game", ...             "DeveloperTeamSize": ... Read More

MongoDB query to get specific month|year (not date)?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

599 Views

You can use aggregate framework along with $month projection operator. Let us first create a collection with documents −> db.specificMonthDemo.insertOne({"StudentName":"Larry", "StudentDateOfBirth":new ISODate('1995-01-12')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9ca8f1d1b97daf71819") } > db.specificMonthDemo.insertOne({"StudentName":"Chris", "StudentDateOfBirth":new ISODate('1999-12-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9db8f1d1b97daf7181a") } > db.specificMonthDemo.insertOne({"StudentName":"David", "StudentDateOfBirth":new ISODate('2000-06-01')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb9a9ee8f1d1b97daf7181b") }Following is the query to display all documents from the collection with the help of find() method −> db.specificMonthDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cb9a9ca8f1d1b97daf71819"),    "StudentName" : "Larry",    "StudentDateOfBirth" : ISODate("1995-01-12T00:00:00Z") } {    "_id" ... Read More

How do I add a value to the top of an array in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

155 Views

To add a value to the top of an array in MongoDB, you can use unshift() −yourArrayName.unshift("yourValue");The above syntax will add the value to the top of an array in MongoDB. Let us first create an array of strings −> technicalSkills=["Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL"];This will produce the following output −[ "Java", "MySQL", "C", "SQL SERVER", "ORACLE", "PL/SQL" ]Following is the query to add to the top of an array in MongoDB. Here, we will add “MongoDB” to top of an array using unshift() −> technicalSkills.unshift("MongoDB"); This will produce the following output −7Let us check the “MongoDB” has ... Read More

Finding matching records with LIKE in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

90 Views

Let us first create a collection with documents −> db.likeDemo.insertOne({"Name":"John", Age:32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb84984623186894665ae41") } > db.likeDemo.insertOne({"Name":"Chris", Age:25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb84991623186894665ae42") } > db.likeDemo.insertOne({"Name":"Carol", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849a1623186894665ae43") } > db.likeDemo.insertOne({"Name":"Johnny", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849b2623186894665ae44") } > db.likeDemo.insertOne({"Name":"James", Age:27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb849bb623186894665ae45") }Following is the query to display all documents from the collection with the help of find() method −> db.likeDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

Converting isodate to numerical value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

349 Views

You can use getTime() for this. Following is the syntax −yourVariableName.getTime();Convert ISODate to numerical value −> var arrivalDate=ISODate('2019-04-18 13:50:45');Following is the query to convert ISODate to numerical value −> arrivalDate.getTime();This will produce the following output −1555595445000 Let us verify that it is a correct numerical value for ISODate or not. Following is the query to get correct ISODate whenever we apply above numeric value −> new Date(1555595445000);This will produce the following output −ISODate("2019-04-18T13:50:45Z")Yes, this is a correct ISODate.

MongoDB query to find property of first element of array?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

968 Views

You can use $slice operator for this. Let us first create a collection with documents −> db.firstElementOfArray.insertOne( ...    { ...       _id: 100, ...       "Details": [ ...          { ...             "CustomerName": "John", ...             "CustomerCountryName":"US" ...          } ...       ] ...    } ... ); { "acknowledged" : true, "insertedId" : 100 } > db.firstElementOfArray.insertOne( ...    { ...       _id: 101, ...       "Details": [ ...       ... Read More

Creating alias in a MongoDB query?

Samual Sam
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can use aggregate framework to create an alias. Let us first create a collection with documents −> db.creatingAliasDemo.insertOne({_id:101, "Name":"John Doe"}); { "acknowledged" : true, "insertedId" : 101 } > db.creatingAliasDemo.insertOne({_id:102, "Name":"David Miller"}); { "acknowledged" : true, "insertedId" : 102 } > db.creatingAliasDemo.insertOne({_id:103, "Name":"Sam Williams"}); { "acknowledged" : true, "insertedId" : 103 }Following is the query to display all documents from the collection with the help of find() method −> db.creatingAliasDemo.find().pretty();This will produce the following output −{ "_id" : 101, "Name" : "John Doe" } { "_id" : 102, "Name" : "David Miller" } { "_id" : 103, "Name" : ... Read More

Difference between find() and findOne() methods in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

4K+ Views

The findOne() returns first document if query matches otherwise returns null. The find() method does not return null, it returns a cursor.Let us implement the concept of find() and findOne() and create a collection with documents −> db.createCollection('emptyCollection'); { "ok" : 1 }Let us count how many documents are in the above collection −> db.emptyCollection.count();This will produce the following output −0There is no document present in the above collection.Following is the query to check the result with findOne() −> if(db.emptyCollection.findOne()){print("Returns Cursor")} else {print("Not returning cursor")} This will produce the following output −Not returning cursorFollowing is the query to check the ... Read More

How to remove a MySQL collection named 'group'?

Samual Sam
Updated on 30-Jul-2019 22:30:25

54 Views

The group is a type of method in MongoDB. It shouldn’t be created as a collection name. Even if you created it, you can easily remove it using getCollection('group').drop();). Let us first create a collection with documents −> db.createCollection('group'); { "ok" : 1 } > db.getCollection('group').insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80fe9623186894665ae3c") } > db.getCollection('group').insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80fef623186894665ae3d") }Following is the query to display all documents from the collection with the help of find() method −> db.getCollection('group').find().pretty();This will produce the following output −{ "_id" : ObjectId("5cb80fe9623186894665ae3c"), "StudentName" : "Chris" } { ... Read More

Advertisements