Database Articles

Page 484 of 546

How to query a key having space in its name with MongoDB?

George John
George John
Updated on 30-Jul-2019 2K+ Views

To query a key having space in its name, you can use dot(.) notation.Step 1: First, you need to create a set in which a key has space in its name. Following is the query:> myValues["Details"] = {} { } > myValues["Details"]["Student Name"]="John"; John > myValues["Details"]["StudentAge"]=26; 26Step 2: Now you need to create a collection and store the above set as a document. Following is the query> db.keyHavingSpaceDemo.insertOne( myValues); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca27e3b6304881c5ce84ba4") }Following is the query to display all documents from a collection with the help of find() method> db.keyHavingSpaceDemo.find().pretty();This will produce the following ...

Read More

How to project specific fields from a document inside an array in Mongodb?

George John
George John
Updated on 30-Jul-2019 378 Views

To project specific fields from a document inside an array, you can use positional ($) operator.Let us first create a collection with documents> db.projectSpecificFieldDemo.insertOne(    ... {       ... "UniqueId": 101,       ... "StudentDetails" : [{"StudentName" : "Chris", "StudentCountryName ": "US"},          ... {"StudentName" : "Robert", "StudentCountryName" : "UK"},       ... ]       ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca27aeb6304881c5ce84ba2") } > db.projectSpecificFieldDemo.insertOne( { "UniqueId": 102, "StudentDetails" :    [{"StudentName" : "Robert", "StudentCountryName ": "UK"}, {"StudentName" : "David",    "StudentCountryName" : "AUS"}, ] ...

Read More

How to drop a numeric collection from MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 198 Views

In order to remove the numeric collection name, use the following syntaxdb.getCollection("yourNumericCollectionName").drop();First, create a numeric collection. Following is the query> db.createCollection("2536464"); { "ok" : 1 }Now insert some documents in the above collection. Following is the query> db.getCollection("2536464").insertOne({"Record":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a46304881c5ce84b8e") } > db.getCollection("2536464").insertOne({"Record":2}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a76304881c5ce84b8f") } > db.getCollection("2536464").insertOne({"Record":3}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca254a96304881c5ce84b90") }Following is the query to display all documents from a collection with the help of find() method> db.getCollection("2536464").find().pretty();This will produce the following output{ "_id" : ObjectId("5ca254a46304881c5ce84b8e"), "Record" : 1 ...

Read More

MongoDB equivalent of WHERE IN(1,2,…)?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 267 Views

The MongoDB equivalent of WHERE IN(1, 2, ....) is $in operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$in:[yourValue1, yourValue2, ....N]}}).pretty();Let us first create a collection with documents> db.whereInDemo.insertOne({"StudentName":"John", "StudentMathScore":57}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281ec6304881c5ce84ba5") } > db.whereInDemo.insertOne({"StudentName":"Larry", "StudentMathScore":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281f56304881c5ce84ba6") } > db.whereInDemo.insertOne({"StudentName":"Chris", "StudentMathScore":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca281fd6304881c5ce84ba7") } > db.whereInDemo.insertOne({"StudentName":"Robert", "StudentMathScore":99}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2820a6304881c5ce84ba8") } > db.whereInDemo.insertOne({"StudentName":"Bob", "StudentMathScore":97}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca282206304881c5ce84ba9") }Following is the query to display all documents from a collection with ...

Read More

How to return static strings in MySQL?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

In order to return static strings in MySQL, you can use UNION. Following is the syntax −select 'yourStringValue1' as yourAliasName UNION select 'yourStringValue2' as yourAliasName;Let us implement the above syntax to return static strings in MySQL. Following is the query −mysql> select 'HELLO' as staticStringsResult    -> UNION    -> select 'MySQL' as staticStringsResult;This will produce the following output −+---------------------+ | staticStringsResult | +---------------------+ | HELLO             | | MySQL             | +---------------------+ 2 rows in set (0.00 sec)In some MySQL versions, the above syntax does not work, therefore you ...

Read More

MongoDB order by two fields sum?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 522 Views

To order by two fields sum, you can use the aggregate framework. Let us first create a collection with documents> db.orderByTwoFieldsDemo.insertOne({"Value1":10, "Value2":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca285576304881c5ce84baa") } > db.orderByTwoFieldsDemo.insertOne({"Value1":12, "Value2":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2855f6304881c5ce84bab") } > db.orderByTwoFieldsDemo.insertOne({"Value1":55, "Value2":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca285686304881c5ce84bac") }Following is the query to display all documents from a collection with the help of find() method> db.orderByTwoFieldsDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca285576304881c5ce84baa"),    "Value1" : 10,    "Value2" : 35 } {    "_id" : ObjectId("5ca2855f6304881c5ce84bab"),    "Value1" ...

Read More

How to search for “ñ” and avoid records that include “n” in MySQL?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 574 Views

If you do not want all records that include “n” when you search for “ñ”, use the following syntax −select *from yourTableName where yourColumnName LIKE '%ñ%' COLLATE utf8_spanish_ci;Let us first create a table. Following is the query −mysql> create table NotIncludenDemo    -> (    -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> ClientName varchar(20)    -> ); Query OK, 0 rows affected (1.07 sec)Following is the query to insert some records in the table using insert command −mysql> insert into NotIncludenDemo(ClientName) values('John'); Query OK, 1 row affected (0.21 sec) mysql> insert into NotIncludenDemo(ClientName) values('Johñ'); Query OK, ...

Read More

Finding highest value from sub-arrays in MongoDB documents?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 287 Views

To find the highest value from sub-arrays in documents, you can use an aggregate framework. Let us first create a collection with documents> db.findHighestValueDemo.insertOne(    ... {       ... _id: 10001,       ... "StudentDetails": [          ... { "StudentName": "Chris", "StudentMathScore": 56},          ... { "StudentName": "Robert", "StudentMathScore":47 },          ... { "StudentName": "John", "StudentMathScore": 98 }]    ... } ... ); { "acknowledged" : true, "insertedId" : 10001 } > db.findHighestValueDemo.insertOne(    ... {       ... _id: 10002,       ... "StudentDetails": [ ...

Read More

Set Blank spaces in column names with MySQL?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 1K+ Views

To set blank spaces in column names with MySQL, you can use the concept of backticks. Let us first create a table. Following is the query −mysql> create table blankSpacesDemo    -> (    -> `Student Id` int NOT NULL AUTO_INCREMENT PRIMARY KEY,    -> `Student Full Name` varchar(100)    -> ); Query OK, 0 rows affected (0.51 sec)Following is the query to insert some records in the table using insert command −mysql> insert into blankSpacesDemo(`Student Full Name`) values('John Smith'); Query OK, 1 row affected (0.16 sec) mysql> insert into blankSpacesDemo(`Student Full Name`) values('Carol Taylor'); Query OK, 1 row ...

Read More

Get number of updated documents in MongoDB?

George John
George John
Updated on 30-Jul-2019 315 Views

To get number of updated documents in MongoDB, you need to use runCommand along with getlasterror.Let us first create a collection with documents> db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c1d6304881c5ce84bad") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c226304881c5ce84bae") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c276304881c5ce84baf") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Ramit"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c366304881c5ce84bb0") } > db.getNumberOfUpdatedDocumentsDemo.insertOne({"StudentName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca28c436304881c5ce84bb1") }Following is the query to display all documents from a collection with the help of find() method:> db.getNumberOfUpdatedDocumentsDemo.find().pretty();This will ...

Read More
Showing 4831–4840 of 5,456 articles
« Prev 1 482 483 484 485 486 546 Next »
Advertisements