http://try.mongodb.org/
大家可以去上面这个地址实际操作下。
JavaScript Shell
The first thing to notice is that the MongoDB shell is JavaScript-based.
So you can do things like:
a = 5;
a * 10;
for(i=0; i<10; i++) { print('hello'); };
2. Documents
MongoDB is a document database. This means that we store data as documents,
which are similar to JavaScript objects. Here below are a few sample JS objects:
var a = {age: 25};
var n = {name: 'Ed', languages: ['c', 'ruby', 'js']};
var student = {name: 'Jim', scores: [75, 99, 87.2]};
3. Saving
Here's how you save a document to MongoDB:
db.scores.save({a: 99});
This says, "save the document '{a: 99}' to the 'scores' collection."
Go ahead and try it. Then, to see if the document was saved, try
db.scores.find();
4. Saving and Querying
Try adding some documents to the scores collection:
for(i=0; i<10; i++) { db.scores.save({a: i, exam: 5}) };
Try that, then enter
db.scores.find();
to see if the save succeeded. Since the shell only displays 10 results at time,
you'll need to enter the 'it' command to iterate over the rest.
5. Basic Queries
You've already tried a few queries, but let's make them more specific.
How about finding all documents where a == 2:
db.scores.find({a: 2});
Or what about documents where a > 15?
db.scores.find({a: {'$gt': 15}});
6. Query Operators
Query Operators:
$gt is one of many special query operators. Here are few others:
$lt - '<', $lte - '<=',
$gte - '>=', $ne - '!='
$in - 'is in array', $nin - '! in array'
db.scores.find({a: {'$in': [2, 3, 4]}});
db.scores.find({a: {'$gte': 2, '$lte': 4}});
7. Updates
Now create a couple documents like these for updating:
db.users.save({name: 'Johnny', languages: ['ruby', 'c']});
db.users.save({name: 'Sue', languages: ['scala', 'lisp']});
Make sure they were saved by called db.users.find()
Update the first document like so:
db.users.update({name: 'Johnny'}, {name: 'Cash', languages: ['english']});
8. Update Operators
The previous update replaced the entire document, but MongoDB also
supports partial updates to documents. For example, you can set a value:
db.users.update({name: 'Cash'}, {'$set': {'age': 50} });
You can also push and pull items from arrays:
db.users.update({name: 'Sue'}, {'$pull': {'languages': 'scala'} });
db.users.update({name: 'Sue'}, {'$push': {'languages': 'ruby'} });
9. Deleting data
To delete matching documents only, add a query selector to the remove method:
db.users.remove({name: 'Sue'});
To delete everything from a collection:
db.scores.remove();
分享到:
相关推荐
首先,让我们深入了解MongoDB的基本概念。MongoDB基于JSON(JavaScript Object Notation)文档存储数据,这种格式使得数据的存储和查询更为直观。每个文档都是一组键值对,类似于JavaScript的对象。文档被组织在集合...
作为一个初学者,了解如何安装和操作 MongoDB 是非常基础且重要的。下面将详细解释 MongoDB 的安装过程以及一些基本的 Shell 操作。 首先,安装 MongoDB 需要访问官方网站下载对应系统的安装包。对于 Windows 用户...
MongoDB初体验 MongoDB是一种流行的开源、分布式文档数据库,属于NoSQL数据库家族,它以其灵活性、可扩展性和高性能而受到广泛关注。这篇文章将带你初探MongoDB的世界,了解其基本概念、安装过程以及如何进行基本...
初入职场 最开始就是纯后端开发,当年入行的时候还是SSH(struct2+spring mvc+hibernate)三件套,相信这一套东西早就消失在如今的开发模式中了。之后随着微服务概念兴起,大家开始拆分服务,自然而然引入了 注册中心...
这包括连接到MongoDB服务器,使用`mongo`命令行工具,以及熟悉其shell环境。在MongoDB shell中,我们可以执行CRUD(创建、读取、更新、删除)操作,对集合(类似于关系数据库中的表)进行管理。例如,创建一个新的...
这个项目的描述提到"节点mongo注册验证API",意味着它涉及到MongoDB数据库的集成以及用户注册和验证的功能。MongoDB是一个流行的NoSQL数据库,常用于存储非结构化或半结构化的数据,如JSON文档。在后端开发中,它...
通过学习这个项目,开发者不仅可以掌握Spring WebFlux的反应式编程,还能了解到Spring Cloud生态中的核心组件,以及如何与MongoDB进行高效的数据交互。对于想要深入理解微服务架构和反应式编程的开发者来说,这是一...