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. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  2. Android在layout xml中使用include

    Android include与merge标签使用详解 - shuqiaoniu的博客 - 博客频道 - CSDN.NEThttp://blog.csdn.net/shuqiaoniu/article ...

  3. netbeans-xdebug 断点调试php

    来自NetBeans官网的帮助文档: https://netbeans.org/kb/docs/php/debugging_zh_CN.html 但具体问题,我们还是要说下 准备工作 本地部署的ser ...

  4. UE3名称结构(Name)

    解释说明: (1) 直接通过FName的index进行比较来判断两个FName是否相等 (2) 通过FName的index从全局Names数组中取出对应的FNameEntry,可以获得FName的字符 ...

  5. [UWP]使用AdaptiveTrigger实现自适应布局

    这篇博客将介绍如何在UWP开发中使用AdaptiveTrigger实现自适应布局. 场景1:窗体宽度大于800时,窗体背景色为绿色,窗体在0到800之间为蓝色. XAML Code: <Grid ...

  6. C语言语法 typedef小结

    在总结typedef之前,先了解一个专业术语: 常量指针(const pointer):常量指针在定义的时候必须被初始化,而且一旦初始化完成,则它的值就不能再改变. int errNumb = 0; ...

  7. 更改conda安装源镜像

    为了更好安装包需要制定conda安装包所使用的镜像为中国的镜像,目前为止只有一家提供了镜像 执行conda命令: conda config 会创建conda的配置文件,使用search everyth ...

  8. SQL函数说明大全

    一旦成功地从表中检索出数据,就需要进一步操纵这些数据,以获得有用或有意义的结果.这些要求包括:执行计算与数学运算.转换数据.解析数值.组合值和聚合一个范围内的值等. 下表给出了T-SQL函数的类别和描 ...

  9. Qt中 QString 和int, char等的“相互”转换

    转载:http://blog.csdn.net/ei__nino/article/details/7297791 Qt中 int ,float ,double转换为QString 有两种方法 1.使用 ...

  10. Blend打不开wpf项目,提示无法识别的工具版本“12.0”

          讲项目文件用记事本打开然后修改为“14.0”即可