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. Idea下Python开发平台的搭建

    1. python的下载 https://www.python.org/downloads/ 2. idea下python插件的安装 点击File->Settings...->Plugin ...

  2. springboot2.X整合mybatis

    github地址:https://github.com/BenchChen/springboot 1) 创建springboot-maven项目,并修改pom文件 <?xml version=& ...

  3. maven编译错误,警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除

    修改红色部分版本号为2.3.2              <plugin>                 <groupId>org.apache.maven.plugins& ...

  4. linux系统编程:守护进程详解及创建,daemon()使用

    一,守护进程概述 Linux Daemon(守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.它不需要用户输入就能运行而且提供某种服务,不是对整个 ...

  5. TensorFlow进阶(四)---名称域和共享变量

    变量作用域 tensorflow提供了变量作用域和共享变量这样的概念,有几个重要的作用. 让模型代码更加清晰,作用分明 变量作用域域 通过tf.variable_scope(<scope_nam ...

  6. 关于vs2013调试的偶然错误发现与总结(vs2013的承载进程)---ShinePans

    当项目的属性选择为 启用 vs2013承载进程 或出现一下错误: 尝试运行项目时出错:未能加载文件或程序集"GroupBoxTest" 或它的某一个依赖项.给定程序集名称" ...

  7. 【Python】Django用户、认证、鉴权模块使用

    此文是总结Django官方网站里面的Document的文章 User authentication in Django http://www.djangoproject.com/documentati ...

  8. EasyUI-EasyUI框架入门学习

    前言 新项目的开发前端技术打算采用EasyUI框架(基于EasyUI较为丰富的UI组件库),项目组长将前端EasyUI这块的任务分配给了我.在进行开发之前,需要我这菜鸟对EasyUI框架进行一些基础的 ...

  9. Transform数据权限浅析1之mdl语句批量加载权限

    Cognos建模工具除了Framework之外,还有一个Transform,两者的最大区别就是在于Framework是通过结构直连关系数据库的,数据根据数据仓库的变化而变化,而Transform是生产 ...

  10. 彻底领悟javascript中的exec与match方法

    exec是正则表达式的方法,而不是字符串的方法,它的参数才是字符串,如下所示: var re=new RegExp(/\d/); re.exec( "abc4def" ); //或 ...