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的更多相关文章

  1. 在express中使用Mongoose连接MongoDB

    为何要学Mongoose? Mongoose是MongoDB的一个对象模型工具,封装了MongoDB对文档的的一些增删改查等常用方法,让NodeJS操作Mongodb数据库变得更加灵活简单. 0.安装 ...

  2. nodejs(一) 简单登录验证 使用mongoose 操作MongoDB

    ---恢复内容开始--- 开发使用webstorm 9  新建nodejs+express 项目 newfarmer 文章目录 配置Mongoose 创建目录及文件 插入数据,POST提交JSON增加 ...

  3. node 通过mongoose实现 mongodb的增删改

    node 通过mongoose实现 mongodb的增删改   新建文件test.js 内容如下:   var mongoose = require('mongoose') , Schema = mo ...

  4. Node使用Mongoose操作MongoDB数据库——增删改查的实现

    当初刚出社会时就规划了下自己的职业生涯:先成为一名优秀的前端工程师,再成为一名全栈工程师(精通前端开发.后台开发和客户端开发),最后成为一名优秀的系统架构师.转眼间已经工作快三年,是时候迈出关键性的一 ...

  5. nodejs 使用mongoose 操作mongodb

    nodejs操作mongodb可以使用mongoose: Mongoose is a MongoDB object modeling tool designed to work in an async ...

  6. nodejs操作 mongoose(mongodb)和Sequelize(mysql)查询数据后添加新属性未生效

    最近在着手koa时候,发现mongoose(mongodb)查询数据库后添加新属性,前端拿不到新属性问题, 然后测试了一下Sequelize(mysql),发现也有同样的问题存在.此时着手干! 1.1 ...

  7. 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 ...

  8. koa项目用mongoose与mongodb交互,始终报错FormModel is not defined

    koa项目用mongoose与mongodb交互,始终报错FormModel is not defined,就是自己定义的model实例始终不能找到,但是明明定义了,这时候就要看大小写了,当创建一个m ...

  9. 用mongoose实现mongodb增删改查

    //用户 var mongoose = require("mongoose"), setting = require("./setting"); //配置连接数 ...

  10. 使用mongoose操作mongodb数据库

    1.如何启动mongodb数据库 参考地址:http://www.runoob.com/mongodb/mongodb-window-install.html 在数据库安装的地方,bin文件夹,输入 ...

随机推荐

  1. css 上下滚动效果

    <html> <head> <style> .scroll{ overflow:hidden; width:100%; } .scrollout{ height:2 ...

  2. Physics(物理系统)

    物理: Physics            Box2d   Unity 内置NVDIA PhysX物理引擎 刚体:要使一个物体在物理控制下,简单添加一个刚体给它.这时,物体将受重力影响,并可以与其他 ...

  3. ******IT公司面试题汇总+优秀技术博客汇总

    滴滴面试题:滴滴打车数据库如何拆分 前端时间去滴滴面试,有一道题目是这样的,滴滴每天有100万的订单,如果让你去设计数据库,你会怎么去设计? 当时我的想法是根据用户id的最后一位对某个特殊的值取%操作 ...

  4. Oracle跟踪文件

    1.跟踪文件分类 1)计划内的.由用户请求所产生的跟踪文件 2)计划外的.数据库服务器自动产生的跟踪文件 2.计划内的.由用户请求所产生的跟踪文件 2.1 生成 ①alter session set ...

  5. [Tool]Inno Setup创建软件安装程序。

    这篇博客将介绍如何使用Inno Setup创建一个软件安装程序. Inno Setup官网:http://www.jrsoftware.org/isinfo.php. 可以下载到最新的Inno Set ...

  6. maven权威指南学习笔记(四)—— maven生命周期(lifecycle)

    定义: 生命周期是包含在一个项目构建中的一系列有序的阶段 举个例子来说就是maven 对一个工程进行: 验证(validate) -- 编译源码(compile) -- 编译测试源码(test-com ...

  7. Express URL跳转(重定向)的实现

    Express URL跳转(重定向)的实现   Express是一个基于Node.js实现的Web框架,其响应HTTP请求的response对象中有两个用于URL跳转方法res.location()和 ...

  8. 前端js调用七牛制作评价页面案例

    一.需求 公司所有的上传页面都用七牛,前端不免要直接调用七牛的代码进行上传,以下是一个实现七牛上传的案例,制作一个常见的商品评价页面,页面需求很常见当上传到第五章图片的时候,上传按钮消失,上传需要显示 ...

  9. 批发零售车销门店扫描打印一体移动销售POS机-移动终端销售O2O新模式

    应用领域 终端及移动解决方案 方案概述 通过手持终端对数据进行采集并分析及汇总.利用WIFI网络和专用终端,实时上报终端的各种销量数据,如订单数据.销量数据.库存数据.补货数据.调货数据等. 业务价值 ...

  10. 6种方法实现asp.net返回上一页

    其实要实现返回上一页的功能,主要还是要用到JavaScript. 一: 在ASP.net的aspx里面的源代码中 <input type="button onclick="J ...