- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to query a key having space in its name with MongoDB?
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; 26
Step 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 output
{ "_id" : ObjectId("5ca27e3b6304881c5ce84ba4"), "Details" : { "Student Name" : "John", "StudentAge" : 26 } }
Here is how you can query a key having space in its name i.e. “Student Name”. Following is the query
> db.keyHavingSpaceDemo.find({ "Details.Student Name": "John"} ).pretty();
This will produce the following output
{ "_id" : ObjectId("5ca27e3b6304881c5ce84ba4"), "Details" : { "Student Name" : "John", "StudentAge" : 26 } }
Advertisements