mongoose 和 mongoDB
mongodb传送门:
# 第三方学习地址:
http://blog.csdn.net/foruok/article/details/47746057 # 下载mongoDB
https://www.mongodb.com/download-center
下载后默认是在:C:\Program Files\MongoDB\Server\3.2\bin
进入bin目录,使用以下命令开启数据库服务(需要先创建E:\MongoDB_Path)
$ ./mongod --dbpath "E:\MongoDB_Path"
此时界面会停在2015-03-26T15:19:17.135+0800 I NETWORK [initandlisten] waiting for connections on port 27017 (此时数据库就已经启动)
nodejs mongodb库传送门:
# nodejs mongodb库 github 与 官网
https://github.com/mongodb/node-mongodb-native
http://mongodb.github.io/node-mongodb-native/ # nodejs mongoose库 github
https://github.com/Automattic/mongoose
http://mongoosejs.com
安装mongoose : npm install mongodb
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = ; let server = new mongo.Server(host,port,{auto_reconnect:true});
let db = new mongo.Db('node-mongo-examples',server,{safe:true}); db.open(function(err,db){
if(err) console.log("err");
else {
console.log("成功建立数据库链接");
db.close();
} db.on('close',function(err,db){
if(err) console.log("关闭数据库失败");
else console.log("关闭数据库成功");
})
})
数据插入
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; var db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,data){
db.collection('users',function(err,collection){
collection.insert({
username:"李钊鸿",
nickname:"贝尔塔猫"
},function(err,docs){
console.log(docs);
db.close();
})
})
})
查询数据
let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db){
db.collection('users',function(err,collection){
if(err) throw err;
else {
collection.find({}).toArray(function(err, docs){
console.log(docs);
db.close();
})
}
})
}) /* 指定查询的字段和值 */
{username:"李钊鸿"} /* 指定查询的字段并限定字段值的范围 */
{username:{$in:['Lee','李钊鸿']}} /* 指定查询字段,0为忽略,1为包含 默认_id是包含的 */
{username:"李钊鸿"},{fields:{username:1,_id:0}}
插入多个值,同时指定多个字段的查询条件
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; var docs = [
{type:"food",price:11},
{type:"food",price:10},
{type:"food",price:9},
{type:"food",price:8},
{type:"book",price:7}
];
/* 多字段查询 */
let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db){
db.collection('goods',function(err,collection){
collection.insert(docs,function(err, docs){
if(err) throw err;
else {
collection.find({type:"food",price:{$lt:10}}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(docs);
db.close();
}
})
}
})
})
}) /* 或查询 */
let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db)
{
db.collection('goods',function(err,collection){
collection.find({$or:[{type:"food"},{price:{$lt:10}}]}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(docs);
db.close();
}
})
})
}) /* 且与或联合查询 */
db.open(function(err,db)
{
db.collection('goods',function(err,collection){
collection.find({type:"food",$or:[{price:11},{price:{$lt:9}}]}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(docs);
db.close();
}
})
})
})
在查询条件中指定一个数组的完整内容
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let article1 = {name:"TV",tags:['device','electric equipment']};
let article2 = {name:"apple",tags:['fruit','food','citrus']};
let article3 = {name:"Node.js",tags:['language','web','computer']};
var docs = [article1,article2,article3]; var db = new mongo.Db('node-mogo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,data){
db.collection('articles',function(err,collection){
collection.insert(docs,function(err, docs){
if(err) throw err;
else {
collection.find({tags:['fruit','food','citrus']}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(docs);
db.close();
}
})
}
})
})
}) /* 除了可以指定数组完整内容外。还可以单独指定字段值数组中包含的某个元素 */
{'tags':'citrus'} /* 使用数组中的序号来精确指定字段值数组(第一个元素的序号为0) */
{'tags.0':'fruit'}
指定某个子数据文档的某个元素的查询条件
"use strict"
const util = require('util');
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let food1 = {type:"food",price:11};
let food2 = {type:"food",price:10};
let food3 = {type:"food",price:9};
let food4 = {type:"food",price:8};
let food = [food1,food2,food3,food4];
let store1 = {name:"store1",goods:food}; let book1 = {type:"book",price:11};
let book2 = {type:"book",price:10};
let book3 = {type:"book",price:9};
let book4 = {type:"book",price:8};
let book = [book1,book2,book3,book4];
let store2 = {name:"store2",goods:book}; var storesArray = [store1,store2]; var db = new mongo.Db('node-mogo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,data){
db.collection('stores',function(err,collection){
collection.insert(storesArray,function(err, docs){
if(err) throw err;
else {
collection.find({'goods.type':"book"}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(util.inspect(docs,{depth:3}));
db.close();
}
})
}
})
})
}) /* 小于$lt */
{'goods.price':{$lt:10}} /* 倒序排列,从大到小 */
/* {},{sort:{price:-1}} */ /* limit */
{},{limit:1} /* explain 查看性能 */
{},{explain:true}
指定在查询时利用根据price 字段 创建的索引
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db){
db.collection('goods',function(err,collection){
collection.createIndex({price:1},function(err, indexName){
if(err) throw err;
else {
collection.find({type:'food'},{hint:{price:1}}).toArray(function(err,docs){
if(err) throw err;
else {
console.log(docs);
db.close();
}
})
}
})
})
})
查询一条数据findOne
"use strict"
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db){
db.collection('goods',function(err,collection){
collection.findOne({},function(err , docs){
console.log(docs);
db.close();
})
})
})
Update 更新数据
"use strict"
const util = require('util');
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db)
{
db.collection('users',function(err,collection){
/* {}默认只更新第一条,可能是为了安全考虑考虑吧 */
collection.update({},{username:"test",nickname:"test"},function(err,result){
if(err) throw err;
let n = JSON.parse(result).n;
console.log(`更新了${n}条记录`);
collection.find({}).toArray(function(err,docs){
console.log(docs);
})
})
})
}) /* 指定更新条件 */
{username:"Lee"},{username:"李钊鸿",nickname:"test"}
删除数据
"use strict"
const util = require('util');
const mongo = require('mongodb');
const host = "localhost";
const port = 27017; let db = new mongo.Db('node-mongo-examples',new mongo.Server(host,port,{auto_reconnect:true}),{safe:true});
db.open(function(err,db)
{
db.collection('users',function(err,collection){
collection.remove({username:"test"},function(err,result){
if(err) throw err;
let n = JSON.parse(result).n;
console.log(`更新了${n}条记录`);
collection.find({}).toArray(function(err,docs){
console.log(docs);
})
})
})
})
mongoose 和 mongoDB的更多相关文章
- 在express中使用Mongoose连接MongoDB
为何要学Mongoose? Mongoose是MongoDB的一个对象模型工具,封装了MongoDB对文档的的一些增删改查等常用方法,让NodeJS操作Mongodb数据库变得更加灵活简单. 0.安装 ...
- nodejs(一) 简单登录验证 使用mongoose 操作MongoDB
---恢复内容开始--- 开发使用webstorm 9 新建nodejs+express 项目 newfarmer 文章目录 配置Mongoose 创建目录及文件 插入数据,POST提交JSON增加 ...
- node 通过mongoose实现 mongodb的增删改
node 通过mongoose实现 mongodb的增删改 新建文件test.js 内容如下: var mongoose = require('mongoose') , Schema = mo ...
- Node使用Mongoose操作MongoDB数据库——增删改查的实现
当初刚出社会时就规划了下自己的职业生涯:先成为一名优秀的前端工程师,再成为一名全栈工程师(精通前端开发.后台开发和客户端开发),最后成为一名优秀的系统架构师.转眼间已经工作快三年,是时候迈出关键性的一 ...
- nodejs 使用mongoose 操作mongodb
nodejs操作mongodb可以使用mongoose: Mongoose is a MongoDB object modeling tool designed to work in an async ...
- nodejs操作 mongoose(mongodb)和Sequelize(mysql)查询数据后添加新属性未生效
最近在着手koa时候,发现mongoose(mongodb)查询数据库后添加新属性,前端拿不到新属性问题, 然后测试了一下Sequelize(mysql),发现也有同样的问题存在.此时着手干! 1.1 ...
- Mongoose vs mongodb native driver – what to prefer?
Paul Shan 7th Jun 2015 Mongoose or mongodb native driver, which one to use? This is one of the ini ...
- koa项目用mongoose与mongodb交互,始终报错FormModel is not defined
koa项目用mongoose与mongodb交互,始终报错FormModel is not defined,就是自己定义的model实例始终不能找到,但是明明定义了,这时候就要看大小写了,当创建一个m ...
- 用mongoose实现mongodb增删改查
//用户 var mongoose = require("mongoose"), setting = require("./setting"); //配置连接数 ...
- 使用mongoose操作mongodb数据库
1.如何启动mongodb数据库 参考地址:http://www.runoob.com/mongodb/mongodb-window-install.html 在数据库安装的地方,bin文件夹,输入 ...
随机推荐
- CA扫盲的巅峰之作!!!
★ 先说一个通俗的例子 考虑到证书体系的相关知识比较枯燥.晦涩.俺先拿一个通俗的例子来说事儿. ◇ 普通的介绍信 想必大伙儿都听说过介绍信的例子吧?假设 A 公司的张三先生要到 B 公司去拜访,但是 ...
- cornerstone知识点
CornerStone使用教程(配置SVN,HTTP及svn简单使用) 发布时间:2015-01-02 19:54 作者:芳仔小脚印 来源:开源中国 CornerStone是Mac OS X ...
- 解决Maven项目pom.xml文件报xxx\target\classes\META-INF\MANIFEST.MF (系统找不到指定的路径。)问题
最近自己在公司项目修改一些代码以后,出现如题的错误,后来各种Google等,最终找到了解决办法. 错误环境:Tomcat7 + Eclipse + Maven + Spring + SpringMvc ...
- JS中定义对象原型的两种使用方法
第一种: function Person() { this.username = new Array(); this.password = "123"; } Person.prot ...
- javascript之循环保存数值
javascript之循环保存数值 语言都是相通的,这句话在我学javascript时有的深刻的意识.js中的for循环与java中的for循环有很大相似之处. 先看下面这段代码 for(var i= ...
- js立即调用的函数表达式
1.多种实现 // 下面2个括弧()都会立即执行 (function () { /* code */ } ()); // 推荐使用这个 (function () { /* code */ })(); ...
- 解决浏览器Adobe Flash Player不是最新版本问题
关键:选择谷歌浏览器的PPAPI版本的flash下载直接安装即可 搜索: Adobe Flash Player PPAPI 下载地址: http://www.wmzhe.com/soft-30259. ...
- Swift 之模糊效果(毛玻璃效果,虚化效果)的实现
前言: 之前项目中有用到过Objective-C的的模糊效果,感觉很是不错,而且iOS8之后官方SDK也直接提供了可以实现毛玻璃效果的三个类:UIBlurEffect.UIVibrancyEffect ...
- Arduino下LCD1602综合探究(上)——1602的两种驱动方式,如何使LCD的控制编程变得更简单
一.前言: LCD ( Liquid Crystal Display 的简称)液晶显示器,已经逐渐替代CRT成为主流的显示设备之一,因此也成为了单片机发烧友绕不过的话题之一:而LCD1602更是很多单 ...
- jQuery 遍历函数
转载http://www.cnblogs.com/tylerdonet/archive/2013/04/05/3000618.html jQuery 遍历函数包括了用于筛选.查找和串联元素的方法. 函 ...