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 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
}
}