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);
})
})
})
})

mongoDB 简单使用的更多相关文章

  1. MongoDB学习:(二)MongoDB简单使用

    MongoDB学习:(二)MongoDB简单使用 MongoDB使用: 执行mongodb的操作之前,我们需要运行命令,来进入操作命令界面 >mongo 提示该错误,说明我们系统缺少一个补丁,该 ...

  2. mongodb 简单部署方案及实例

    mongodb 简单部署方案及实例 转载:http://my.oschina.net/zhuzhu0129/blog/53290 第一节 准备工作 一 安装mongodb  我这里选用rehl 5.6 ...

  3. .Net Core MongoDB 简单操作。

    一:MongoDB 简单操作类.这里引用了MongoDB.Driver. using MongoDB.Bson; using MongoDB.Driver; using System; using S ...

  4. python和mongodb简单交互

    python和mongodb简单交互 1.安装pymongo: pip3 install pymongo 2.pymongo的简单用法: # /usr/bin/env python3 import p ...

  5. MongoDB简单CRUD场景

    MongoDB简单CRUD命令操作 (1)新建数据库:use 数据库名 (2)显示所有数据库:show dbs; (3)新建集合(两种方式)  隐式创建:在创建集合的同时往集合里面添加数据---db. ...

  6. NodeJS+Express+MongoDB 简单实现数据录入及回显展示【适合新人刚接触学习】

    近期在看NodeJS相关 不得不说NodeJS+Express 进行网站开发是很不错,对于喜欢玩JS的来说真是很好的一种Web开发组合 在接触NodeJS时受平时Java或者C#中API接口等开发的思 ...

  7. 存储库-MongoDB简单的操作

    简介: MongoDB是一款强大.灵活.且易于扩展的通用型数据库 1.易用性 MongoDB是一个面向文档的数据库,而不是关系型的数据库: 不采用关系型主要是为了可扩展性 2.易扩展性 存储在Mong ...

  8. nodejs+express+mongodb简单的例子

    简单的介绍下node+express+mongodb这三个东西.node:是运行在服务器端的程序语言,表面上看过去就是javascript一样的东西,但是呢,确实就是服务器语言,个人觉得在一定层次上比 ...

  9. scrapy使用MongoDB简单示例

    1.下载安装MongoDBhttps://www.mongodb.com/download-center#community找到合适的版本下载,安装.安装好之后,找到安装目录下D:\Program F ...

  10. MongoDB简单使用 —— 安装

    下载 MongoDB的下载路径为:MongoDB Download Center.Win.Linux.Mac平台的都有,光Win平台的就提供msi和zip绿色版本的,这里我下载的是zip版本的. 命令 ...

随机推荐

  1. 【转】2012年6月26 – 盛大PHP工程师最新面试题

    无笔试. 口试:(前半部分平淡无奇,没什么太难的问题,都是求职岗位基本要会的东西,局限于php.下面是真正进入状态的题.) 谈谈观察者模式是什么?主要应用. 答:类似会有一些server对象时刻侦听某 ...

  2. LINUX开启允许对外访问的网络端口命令

    LINUX通过下面的命令可以开启允许对外访问的网络端口: /sbin/iptables -I INPUT -p tcp --dport 8000 -j ACCEPT #开启8000端口 /etc/rc ...

  3. java序列化 SerializeUtil

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; ...

  4. SpringSecurity3整合CAS实现单点登录

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  5. springBoot-自定义监听器

    package com.cx.springboot.mylistener; import org.springframework.boot.context.event.ApplicationReady ...

  6. ORACLE 中如何截取到时间的年月日中的年

    在Oracle中,要获得日期中的年份,例如把sysdate中的年份取出来,并不是一件难事.常用的方法是:Select to_number(to_char(sysdate,'yyyy')) from d ...

  7. Android 获得view的宽和高

    转自:http://blog.csdn.net/yangdeli888/article/details/25405263 在oncreate()中利用view.getWidth()或是view.get ...

  8. jQuery 对象和 DOM 对象

    jQuery(DOM对象) 或者 $(DOM对象) 此函数的作用是将DOM对象,转换为jQuery的对象 DOM对象其实就是javascript的函数对象,可以用来操作所有HTML元素.比如: a标签 ...

  9. Python Seaborn 笔记

    Seaborn是Python的一个制图工具库,在Matplotlib上构建,支持numpy和pandas的数据结构可视化. 他有多个内置的主题,颜色的主题 可视化单一变量,二维变量用于比较各个变量的分 ...

  10. HTML5+JS手机web开发之jQuery Mobile初涉

    一.起始之语 我一直都是在PC上折腾网页的,这会儿怎么风向周边捣鼓起手机网页开发呢?原因是公司原先使用Java开发的产品,耗了不少人力财力,但是最后的效果却不怎么好.因为,Android系统一套东西, ...