Node js MongoDB简单操作
//win7环境下node要先安装MongoDB的相关组件(非安装MongoDB数据库),在cmd命令行进入node项目目录后执行以下语句
//npm install mongodb //创建连接
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/testdb"; //链接 testdb 库,不存在则创建
MongoClient.connect(url, function(error, db) {
if (error) throw error;
console.log("testdb 数据库已创建!"); var database = db.db("testdb"); //创建集合(表),已有不会报错
database.createCollection('testtable', function (error, resulat) {
if (error) throw error;
console.log("testtable 集合已创建!");
}); //向testtable表插入文档(条数据)
var testdata = { testfield1: "node mongodb", testfield2: "test val" };
database.collection("testtable").insertOne(testdata, function(error, result) {
if (error) throw error;
console.log("文档插入成功");
}); //向testtable表插入多条数据
var testdatas = [
{ testfield1: 'testval1', testfield2: 'testval2', testfield3: '1'},
{ testfield1: 'testval11', testfield2: 'testval22', testfield3: '2'},
{ testfield1: 'testval111', testfield2: 'testval222', testfield3: '3'}
];
database.collection("testtable").insertMany(testdatas, function(error, result) {
if (error) throw error;
console.log("插入的文档数量为: " + result.insertedCount);
}); //创建并向testtable2表插入多条数据
database.collection("testtable2").insertMany(testdatas, function(error, result) {
if (error) throw error;
console.log("插入的文档数量为: " + result.insertedCount);
}); //更新一条数据
var whereStr = {"testfield1":'testval1'}; // 查询条件
var updateStr = {$set: { "testfield2" : "update_testval2" }};
database.collection("testtable").updateOne(whereStr, updateStr, function(error, result) {
if (error) throw error;
console.log("文档更新成功");
});
//更新多条数据
database.collection("testtable").updateMany(whereStr, updateStr, function(error, result) {
if (error) throw error;
//如果满足条件的文档对应值已经是要修改的值,此处更新条数为0
console.log(result.result.nModified + " 条文档被更新");
}); //删除一条数据
var whereStr = {"testfield1":'testval11'}; // 查询条件
database.collection("testtable").deleteOne(whereStr, function(error, object) {
if (error) throw error;
console.log("文档删除成功");
});
//删除多条数据
var whereStr = {"testfield1":'testval111'}; // 查询条件
database.collection("testtable").deleteMany(whereStr, function(error, object) {
if (error) throw error;
console.log(object.result.n + " 条文档被删除");
}); //查询testtable表全部数据
database.collection("testtable"). find().toArray(function(error, result) { // 返回集合中所有数据
if (error) throw error;
console.log(result);
}); //也可按条件查询查询testtable表 testfield1 字段等于 testval1 的信息
var whereStr = {"testfield1":'testval1'}; // 查询条件
database.collection("testtable"). find(whereStr).toArray(function(error, result) {
if (error) throw error;
console.log(result);
}); //查询结果排序
//先按 testfield2 字段升序排列,再按 testfield3 字段降序排列
var sortStr = { testfield2:1, testfield3: -1 };
database.collection("testtable").find().sort(sortStr).toArray(function(error, result) {
if (error) throw error;
console.log(result);
}); //分页查询
//skip(int) 接受一个数字参数,为返回结果中,跳过指定的条数再显示
//limit(int) 接受一个数字参数,为返回结果中,限制显示的条数
//例子将排序后的结果跳过第 1 条后,显示 2 条内容
var sortStr = { testfield2:1, testfield3: -1 };
database.collection("testtable").find().skip(1).limit(2).toArray(function(error, result) {
if (error) throw error;
console.log(result);
}); //多表连接操作
//mongoDB 不是一个关系型数据库,但可以使用 $lookup 来实现左连接
//首先是查询的主表(左表)
database.collection('testtable').aggregate([
{ $lookup:
{
from: 'testtable2', // 关联的右表
localField: 'testfield1', // 左表要关联的 join 字段
foreignField: 'testfield1', // 右表要关联的 join字段
as: 'newfield' // 新生成字段(类型array)
}
}
], function(error, result) {
if (error) throw error;
//console.log(JSON.stringify(result));
console.log(result);
}); //删除表集合
database.collection("testtable2").drop(function(error, delOK) { // 执行成功 delOK 返回 true,否则返回 false
if (error) throw error;
if (delOK) console.log("集合已删除");
}); db.close();
});
Node js MongoDB简单操作的更多相关文章
- Node js MySQL简单操作
//win7环境下node要先安装MySQL的相关组件(非安装MySQL数据库),在cmd命令行进入node项目目录后执行以下语句 //npm install mysql var mysql = re ...
- Node.JS + MongoDB技术浅谈
看到一个Node.JS + MongoDB的小样例,分享给大家.魔乐科技软件学院(www.mldnjava.cn)的讲座 Node.JS + MongoDB技术讲座 云计算 +大数据 ...
- node.js+mongodb 爬虫
demo截图: 本demo爬瓜子二手车北京区的数据 (注:需要略懂 node.js / mongodb 不懂也没关系 因为我也不懂啊~~~) 之所以选择爬瓜子二手车网站有两点: 一.网站无需登录,少做 ...
- 8 步搭建 Node.js + MongoDB 项目的自动化持续集成
任何事情超过 90 秒就应该自动化,这是程序员的终极打开方式.Automating shapes smarter future. 这篇文章中,我们通过创建一个 Node.js + MongoDB 项目 ...
- 《Node.js+MongoDB+AngularJS Web开发》读书笔记及联想
总体介绍 <Node.js+MongoDB+AngularJS Web开发>,于2015年6月出版,是一本翻译过来的书,原书名为<Node.js,MongoDB and Angula ...
- AngularJS + Node.js + MongoDB开发
AngularJS + Node.js + MongoDB开发的基于位置的通讯录(by vczero) 一.闲扯 有一天班长说了,同学们希望我开发一个可以共享位置的通讯录,于是自己简单设计了下功能.包 ...
- .Net Core MongoDB 简单操作。
一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...
- [转]Node.JS使用Sequelize操作MySQL
Sequelize官方文档 https://sequelize.readthedocs.io/en/latest/ 本文转自:https://www.jianshu.com/p/797e10fe23 ...
- node.js + mongodb
node.js + mongodb 这次内容是结合bootstrap把登陆注册做好,还有就是express的中间件等问题. 看这篇博客之前建议先看我上篇写的那篇博客http://www.cnblogs ...
随机推荐
- MFC非模态添加进程控件方法一(线程方法)
由于非模态对话框的自己没有消息循环,创建后无法进行消息处理.需要和父窗口共用消息循环.如果单独在子窗口进行控件由于自己没有单独的消息循环,更新是无法进行的. 如果在父窗口更新控件会造成程序假死.如以下 ...
- Qt上FFTW組件的编译与安裝
Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...
- NOI2002银河英雄传说-带权并查集
[NOI2002]银河英雄传说-带权并查集 luogu P1196 题目描述 Description: 公元五八○一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年, ...
- C/S与B/S 区别以及优缺点
1.什么是C/S结构 C/S (Client/Server)结构,即客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现 ...
- 搭建简单Django服务并通过HttpRequester实现GET/POST http请求提交表单
调试Django框架写的服务时,需要模拟客户端发送POST请求,然而浏览器只能模拟简单的GET请求(将参数写在url内),网上搜索得到了HttpRequester这一firefox插件,完美的实现了模 ...
- 在线大数据cdh迁移,从公有云迁移到私有云
1.添加新节点 2.迁移hdfs和hbase数据: 3.迁移源节点各个服务到新节点: 4.利用HA,包括hdfs,hbase master,resourcemanager的ha实现在线迁移: 5.数据 ...
- c++ 重载运算与类型转换
1. 基础概念 重载的运算符是具有特殊名字的函数:(重载运算符函数,运算符函数.重载运算符) 依次包含返回类型,函数名(operator=),参数列表,函数体. 只有重载的函数调用运算符operato ...
- 基于Impala平台打造交互查询系统
本文来自网易云社区 原创: 蒋鸿翔 DataFunTalk 本文根据网易大数据蒋鸿翔老师DataFun Talk--"大数据从底层处理到数据驱动业务"中分享的<基于Impal ...
- iOS WKWebView添加进度条02
之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条. //添加一个全局属性 @property(nonatomic,strong)CALayer *progresslay ...
- WeTest功能优化第3期:业内首创,有声音的云真机
第3期功能优化目录 [云真机远程调试]音频同步传输实现测试有声 [兼容性测试报告]新增视频助力动态定位问题 [云真机远程调试]菜单栏优化助力机型选择 本期介绍的新功能,秉承创造用户需求的理念,在云真机 ...