|
The above is the query syntax for MongoDB. There is not a separate SQL like language. You just execute JavaScript code, passing documents, which are just JavaScript associative arrays, err, I mean JavaScript objects. To find a particular employee, you do this:- > db.employees.find({name:"Bob"})
复制代码 Bob quit so to find another employee, you would do this:- > db.employees.find({name:"Rick Hightower"})
- { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 }
复制代码 The console application just prints out the document right to the screen. I don't feel 42. At least I am not 100 as shown by this query:- > db.employees.find({age:{$lt:100}})
- { "_id" : ObjectId("4f964d3000b5874e7a163895"), "name" : "Rick Hightower", "gender" : "m", "phone" : "520-555-1212", "age" : 42 }
复制代码 Notice to get employees less than a 100, you pass a document with a subdocument, the key is the operator ($lt), and the value is the value (100). Mongo supports all of the operators you would expect like $lt for less than, $gt for greater than, etc. If you know JavaScript, it is easy to inspect fields of a document, as follows:- > db.employees.find({age:{$lt:100}})[0].name
- Rick Hightower
复制代码 If we were going to query, sort or shard on employees.name, then we would need to create an index as follows:- db.employees.ensureIndex({name:1}); //ascending index, descending would be -1
复制代码 |
|