您的位置:首页 > 数据库 > Mongodb

node学习笔记(六)mongodb数据库

2017-12-26 19:45 225 查看

数据关系

数据库为最大单位,数据库中存放数据的集合,每个集合都是独立的,集合中存放着多条数据,我们可以称这些数据为文档,数据库中,一个文档的键名被称之为字段。

数据库操作

show dbs- 显示存在的数据库

use- 使用和创建数据库

db- 显示当前所在数据库

db.dropDatabase()- 删除当前数据库

集合操作

show collections- 显示存在的集合

db.collection.drop()- 删除集合

db.createCollection()- 创建集合,当对一个集合进行操作的时候,集合如果不存在,将会被自动创建

node下的CRUD操作(CRUD是指增加(Create)、读取查询(Retrieve)、更新(Update)和删除(Delete)几个单词的首字母简写。)

连接数据库

MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
db.close();
});


插入文档

collection.insertMany([
{a : 1}, {a : 2}, {a : 3}
], function(err, result) {
callback(result);
});


更新文档

collection.updateOne({ a : 2 }
, { $set: { b : 1 } }, function(err, result) {
console.log("Updated the document with the field a equal to 2");
callback(result);
});


删除文档

collection.deleteOne({ a : 3 }, function(err, result) {
console.log("Removed the document with the field a equal to 3");
callback(result);
});


查找文档

collection.find({}).toArray(function(err, docs) {
console.log("Found the following records");
callback(docs);
});


注意:先开启mongodb服务进程,最好注册成windows系统服务。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: