Found 1661 Articles for Big Data Analytics

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

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

224 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

128 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

464 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

1K+ 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

4K+ 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

5K+ 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

104 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

How to apply a condition only if field exists in MongoDB?

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

2K+ Views

You can use $or operator for this. Let us first create a collection with documents −> db.applyConditionDemo.insertOne({"StudentName":"Larry", "StudentAge":21, "StudentMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80b78623186894665ae36") } > db.applyConditionDemo.insertOne({"StudentName":"Sam", "StudentAge":23, "StudentMarks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80b87623186894665ae37") } > db.applyConditionDemo.insertOne({"StudentName":"David", "StudentAge":21, "StudentMarks":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80b95623186894665ae38") } > db.applyConditionDemo.insertOne({"StudentName":"Carol", "StudentAge":24, "StudentMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80ba3623186894665ae39") } > db.applyConditionDemo.insertOne({"StudentName":"Chris", "StudentAge":21, "StudentMarks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80bae623186894665ae3a") } > db.applyConditionDemo.insertOne({"StudentName":"Robert", "StudentMarks":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cb80c3d623186894665ae3b") }Following is the ... Read More

Remove null element from MongoDB array?

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

1K+ Views

You can use $pull operator for this. Let us first create a collection with documents −> db.removeNullDemo.insertOne( ... { ...    "_id" : 1, ...    "StudentDetails" : [ ...       { ...          "FirstName": "John", ...          "LastName":"Smith", ... ...       }, ...       { ...          "Age":21 ...       }, ...       null ... ...    ] ... } ... ); { "acknowledged" : true, "insertedId" : 1 } > db.removeNullDemo.insertOne( ... { ...    "_id" : 2, ... ... Read More

Why SHOW DBS does not show my databases in MongoDB?

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

4K+ Views

This SHOW DBS command won’t show the databases because you may have not created a document for a collection. If you will create document for a collection then the created database will be visible.Let us implement the above concept and create a database −> use web; switched to db webFollowing is the query to show all databases −> show dbs;This will produce the following output −admin 0.001GB config 0.000GB local 0.000GB my 0.001GB sample 0.001GB sampleDemo 0.000GB studentSearch 0.000GB test 0.010GB university 0.000GBAbove, the WEB database is not visible since we haven’t created a collection in the same database.In order ... Read More

Advertisements