设为首页 收藏本站
查看: 729|回复: 0

[经验分享] 02-Nodejs与mongodb

[复制链接]

尚未签到

发表于 2017-2-21 11:36:58 | 显示全部楼层 |阅读模式
在上篇中我们介绍了mongodb的安装和配置,下面我们介绍下NodeJs连接MongoDB,这里我们可以联想到我们使用Java的JDBC的处理,当时连接数据库时,需要对应数据库的驱动,这里也一样,需要我们下载NodeJS连接MongoDB的driver。   DSC0000.png
  一、建立连接

    var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});  //数据库 mydb
db.open(function (err, db) {
if (!err) {
console.log('connect');
} else {
console.log(err);
}
});

 如果最终显示connect则说明成功。说明:
对mongodb的collection的操作有两种方法连接collection,分别为:
db.collection('mycoll',function(err,coll){});如果加上{strict:true},则会检查是否存在这个表,如果不存在,则报错。不加这个参数,函数不会在数据库上创建一个集合,直到你真正插入第一个文档。
db.createCollection('mycoll',function(err,coll){});如果加上{strict:true},则如果存在这个集合就会报错。不加这个参数,则如果存在,则忽略创建。
 

   var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
console.log('connect');
db.collection('mycoll', {strict: true}, function (err, collection) {//数据库中表名为 mycoll
if (err) {
console.log(err);
}
});
} else {
console.log(err);
}
});
 注:这里的集合的概念和我们传统的数据库中的表的概念是类似的。


var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
console.log('connect');
db.createCollection('mycoll', {strict: true}, function (err, collection) {
if (err) {
console.log(err);
}
});
} else {
console.log(err);
}
});
   二、删除连接

    var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
console.log('connect');
db.dropCollection('mycoll', {strict: true}, function (err, result) {
console.log(result);
});
} else {
console.log(err);
}
});

  三、CRUD
  1、Insert操作

    var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
db.collection('mycoll', {strict: true}, function (err, collection) {
var tmp1 = {title: 'hello', number: 1};
collection.insert(tmp1, {safe: true}, function (err, result) {
console.log(result);
});
});
} else {
console.log(err);
}
});

  2、Delete操作

    var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
db.collection('mycoll', {strict: true}, function (err, collection) {
collection.remove({title: 'hello'}, {safe: true}, function (err, result) {
console.log(result);
});
});
} else {
console.log(err);
}
});

  3、Update操作

    var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
db.collection('mycoll', {strict: true}, function (err, collection) {
collection.update({title: 'hello'}, {$set: {number: 3}}, {safe: true}, function (err, result) {
console.log(result);
});
});
}
else {
console.log(err);
}
});



  • $inc - increment a particular value by a certain amount
  • $set - set a particular value
  • $unset - delete a particular field (v1.3+)
  • $push - append a value to an array
  • $pushAll - append several values to an array
  • $addToSet - adds value to the array only if its not in the array already
  • $pop - removes the last element in an array
  • $pull - remove a value(s) from an existing array
  • $pullAll - remove several value(s) from an existing array
  • $rename - renames the field
  • $bit - bitwise operations
  4、Select操作

  var mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, {safe: true});
db.open(function (err, db) {
if (!err) {
db.collection('mycoll', {strict: true}, function (err, collection) {
var tmp1 = {title: 'hello'};
var tmp2 = {title: 'world'};
collection.insert([tmp1, tmp2], {safe: true}, function (err, result) {
console.log(result);
});
collection.find().toArray(function (err, docs) {
console.log('find');
console.log(docs);
});
collection.findOne(function (err, doc) {
console.log('findOne');
console.log(doc);
});
});
}
});

  附:
  MongoDB数据类型



  • Float is a 8 byte and is directly convertible to the Javascript type Number

  • Double class a special class representing a float value, this is especially useful when using capped collections where you need to ensure your values are always floats.

  • Integers is a bit trickier due to the fact that Javascript represents all Numbers as 64 bit floats meaning that the maximum integer value is at a 53 bit. Mongo has two types for integers, a 32 bit and a 64 bit. The driver will try to fit the value into 32 bits if it can and promote it to 64 bits if it has to. Similarly it will deserialize attempting to fit it into 53 bits if it can. If it cannot it will return an instance of Long to avoid loosing precession.

  • Long class a special class that let’s you store 64 bit integers and also let’s you operate on the 64 bits integers.

  • Date maps directly to a Javascript Date

  • RegExp maps directly to a Javascript RegExp

  • String maps directly to a Javascript String (encoded in utf8)

  • Binary class a special class that let’s you store data in Mongo DB

  • Code class a special class that let’s you store javascript functions in Mongo DB, can also provide a scope to run the method in

  • ObjectID class a special class that holds a MongoDB document identifier (the equivalent to a Primary key)

  • DbRef class a special class that let’s you include a reference in a document pointing to another object

  • Symbol class a special class that let’s you specify a symbol, not really relevant for javascript but for languages that supports the concept of symbols.
  参考:http://mongodb.github.io/node-mongodb-native/api-articles/nodekoarticle1.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-345222-1-1.html 上篇帖子: NODEJS(5)Expressjs and CRUD 下篇帖子: NODEJS(3)File Upload Sample
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表