Found 6702 Articles for Database

How to remove key fields in MongoDB?

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

1K+ Views

To remove key fields in MongoFB, you can use $unset operator. Let us first create a collection with documents −>db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8289cb58ca2b005e672") } >db.removeKeyFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6c8359cb58ca2b005e673") }Following is the query to display all documents from a collection with the help of find() method −> db.removeKeyFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6c8289cb58ca2b005e672"),    "StudentFirstName" : "John",    "StudentLastName" : "Doe",    "StudentAge" : 23 } {    "_id" : ObjectId("5cc6c8359cb58ca2b005e673"),    "StudentFirstName" : "John",    "StudentLastName" : "Smith",   ... Read More

Retrieve the position in an Array in MongoDB?

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

251 Views

You can use the concept of map reduce to get the position in an array. Let us first create a collection with documents −> db.retrievePositionDemo.find(); { "_id" : ObjectId("5cd569ec7924bb85b3f4893f"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] }Following is the query to display all documents from a collection with the help of find() method −> db.retrievePositionDemo.find().pretty();This will produce the following output −{     "_id" : ObjectId("5cd569ec7924bb85b3f4893f"),     "Subjects" : [        "MySQL",        "MongoDB",         "Java"     ] }Following is the query to retrieve the position in an array in MongoDB ... Read More

MongoDB multidimensional array projection?

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

525 Views

For MongoDB multidimensional array projection, you need to use aggregate framework. Let us first create a collection with documents. Here, we have multidimensional array for Student marks −> db.multiDimensionalArrayProjection.insertOne( ...    { ...       "StudentFirstName" : "Chris", ...       "StudentMarks" : [ [98, 99], [56, 79] ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b75a9cb58ca2b005e66c") }Following is the query to display all documents from a collection with the help of find() method −> db.multiDimensionalArrayProjection.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cc6b75a9cb58ca2b005e66c"),    "StudentFirstName" : "Chris",    "StudentMarks" ... Read More

How to maintain the top count of array elements in MongoDB?

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

92 Views

You can use aggregate framework. Let us first create a collection with documents −> db.topCountArrayDemo.insertOne( ...    {"StudentId":101 , "StudentSubject": ["C", "MongoDB"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3209cb58ca2b005e669") } > db.topCountArrayDemo.insertOne( ...    {"StudentId":102 , "StudentSubject": ["C", "Java"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3219cb58ca2b005e66a") } > db.topCountArrayDemo.insertOne( ...    {"StudentId":103 , "StudentSubject": ["C", "MongoDB"]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc6b3229cb58ca2b005e66b") }Following is the query to display all documents from a collection with the help of find() method −> db.topCountArrayDemo.find().pretty();This will produce the following output −{ ... Read More

How to create a user in MongoDB v3?

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

131 Views

To create a user in MongoDB v3, use the createUser() method. This allows you to create a user and while creating you need to add user, password, and roles as well. These roles assign permissions. The syntax is as follows −use admin db.createUser(    {       user: “yourUserName",       pwd: "yourPassword",       roles: [ { role: "yourPermission", db: "yourDatabase" } ]    } );Let us implement the above syntax in order to create a user In MongoDB v3 −> use admin switched to db admin > db.createUser( ...    { ...       ... Read More

Select two fields and return a sorted array with their distinct values in MongoDB?

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

361 Views

To select two fields and return a sorted array with distinct values, use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.sortedArrayWithDistinctDemo.insertOne( ...    { value1: 4, value2: 5} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e666") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 5, value2: 6} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e667") } > db.sortedArrayWithDistinctDemo.insertOne( ...    {value1: 7, value2: 4} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc690b99cb58ca2b005e668") }Following is the query to display all documents from a collection with the ... Read More

How do I check whether a field contains null value in MongoDB?

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

282 Views

You can use $type operator to check whether a filed contains null value. Let us first create a collection with documents. We have also inserted a null in a field −> db.nullDemo.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a1eac184d684e3fa270") } > db.nullDemo.insertOne({"FirstName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a25ac184d684e3fa271") } > db.nullDemo.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cc68a2cac184d684e3fa272") }Following is the query to display all documents from a collection with the help of find() method −> db.nullDemo.find().pretty();This will produce the following output. One of the field is null −{ "_id" : ObjectId("5cc68a1eac184d684e3fa270"), "FirstName" ... Read More

Use result from MongoDB in shell script?

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

468 Views

Let us first create a collection with a document −>db.useResultDemo.insertOne({"StudentFirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd") }Display all documents from a collection with the help of find() method −> db.useResultDemo.find();Following is the output −{ "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), "StudentFirstName" : "Robert" }Here is the query to use result from MongoDB in shell script with var keyword −> var studentName=db.useResultDemo.findOne({},{_id:0}); > studentNameFollowing is the output −{ "StudentFirstName" : "Robert" }

MySQL Select displaying Data type in a separate column?

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

67 Views

You can use INFORMATION_SCHEMA.COLUMNS for this. Let us first create a table −mysql> create table DemoTable    (    Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    Name varchar(20)    ); Query OK, 0 rows affected (0.73 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable(Name) values('Sam'); Query OK, 1 row affected (0.12 sec)Display all records from the table using select statement −mysql> select *from DemoTable;This will produce the following ... Read More

How can fetch records from specific month and year in a MySQL table?

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

563 Views

Use YEAR() and MONTH() to display records from specific month and year respectively. Let us first create a table −mysql> create table DemoTable    (    CustomerId int NOT NULL AUTO_INCREMENT PRIMARY KEY,    CustomerName varchar(20),    CustomerTotalBill int,    PurchasingDate date    ); Query OK, 0 rows affected (0.83 sec)Insert some records in the table using insert command −mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('John', 2000, '2019-01-21'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('Chris', 1000, '2019-01-31'); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(CustomerName, CustomerTotalBill, PurchasingDate) values('Robert', ... Read More

Advertisements