繁体   English   中英

Mongo无法使用Node.js

[英]Mongo not working with nodejs

我正在处理一个简单的问题,即使用Node.jsMongo数据库中插入集合。 但是我面临两个问题:

1.当我在.insert函数中使用{safe:true}时
(下面: albums.insert(a1, {safe: false}, cb);
albums.insert(a2, {safe: false}, cb); ),则不console.log(doc)集合插入数据库中,即当我执行console.log(doc)时,它们不会在终端上打印(请参阅下面的程序)

2.该程序本身并不会结束,即使它的末尾有db.close() ,我也必须按ctrl + c结束它,并且我可以看到在console.logged终端上打印了“ here” db.close()

非常感谢任何帮助,谢谢!

var Db= require('mongodb').Db,
Connection= require('mongodb').Connection,
Server= require('mongodb').Server,
async= require('async');

var host= "localhost";
var port= Connection.DEFAULT_PORT;

var db= new Db("PhotoAlbums", new Server(host, port, {auto_reconnect: true,
        poolSize: 20}), {w: 1});

var a1= {_id: "Travel", name: "Travel", title: "Travelogues", description:        
"This was great", date: "1/1/2014"}, 
a2= {_id: "friends", name: "friends", title: "Friends", description:   
"Random Pics", dat: "2/1/2014"};

var albums, photos;

async.waterfall([

function(cb)
{
    db.collection("Albums", cb);    
}, 

function (albums_coll, cb)
{
    albums= albums_coll;
    db.collection("Photos", cb);
},

function (photos_coll, cb)
{
    photos= photos_coll;
    albums.insert(a1, {safe: false}, cb);
},

function (doc, cb)
{ 
    console.log("1. Successfully wrote ");
    console.log(doc);
    albums.insert(a2, {safe: false}, cb);   
},

function (docs, cb)
{ 
    console.log("2. Successfully wrote ");
    console.log(docs);
    cb(null);
},

], function(err, results)
{ 
    if(err)
     console.log("ERROR!");
    db.close();
    console.log("here");
});

本质上,您从未连接到数据库。 Db对象需要.open()方法,并且所有交互都必须在该方法的提供的回调中或在“连接”事件处理程序中进行。

还有一些概念,在这里您可能会有点偏离,我想让您直接理解。 首先是修改后的清单:

var async = require('async'),
    MongoClient = require('mongodb').MongoClient;


MongoClient.connect('mongodb://localhost/test',function(err,db) {

  var a1 = {
    "_id": "Travel",
    "name": "Travel",
    "title": "Travelogues",
    "description": "This was great",
    "date": new Date("2014/01/01")
  },
      a2 = {
    "_id": "friends",
    "name": "friends",
    "title": "Friends",
    "description": "Random Pics",
    "date": new Date("2014/01/02")
  };

  var albums;

  async.waterfall(
    [
      function(cb) {
        db.collection("albums",cb);
      },

      function(albums_col,cb) {
        albums = albums_col;
        albums.insert(a1,cb);
      },

      function(doc,cb) {
        console.log("1. Successfully wrote");
        console.log(doc);
        albums.insert(a2,cb);
      },

      function(doc,cb) {
        console.log("2. Successfully wrote");
        console.log(doc);
        cb();
      }
    ],
    function(err,results) {
      if (err) throw err;
      db.close();
    }
  );
});

首先,您应该在新代码中使用MongoClient 这是所有语言的标准实现。 如果需要,您仍然可以使用Server对象,但是连接字符串通常就足够了。 交替:

MongoClient.connect( new Server( 
    "localhost",
    Connection.DEFAULT_PORT,
    { "auto_reconnect": true }
    { w: 1 }
),function(err,db) {

    {...}
});

另一部分是,我将删除默认情况下已设置的选项。 更改连接池选项对于像这样的列表实际上没有任何意义,但是驱动程序中已经内置了默认的5连接,而没有指定此连接。 同样,“ WriteConcern”或{ w: 1 }是默认设置。

其次,在为文档指定的结构中,使用实际日期对象。 这些将作为BSON存储的“日期”类型序列化到MongoDB中,并且在作为真实日期对象读取时也将重新回到您的代码中。 这就是您要处理的日期。 否则,这些只是字符串,并不是很有用。

并不是为集合声明变量,而不是将参数传递给回调,但是为了简洁起见,不喜欢这么做。

请注意, .insert()方法的WriteConcern部分省略了{ safe: true } 除非您真的想覆盖已设置的连接设置(并且您几乎不希望这样做),否则您可能只想使用默认设置。 还不建议使用“安全”选项,因此您应编写等效的{ w: 0 }

但最后要注意的是,如果您确实选择{ w: 0 }那么将不会返回所编写文档的预期“响应”。 即使已将值写入数据库,该值也将报告为null 这是因为这种“不安全”的编写方式不需要数据库的任何确认。 因此,您只是假设它存在,因此可能想要默认值{ w: 1 }已经存在,这意味着已经确认。

然后输出如预期的那样:

1. Sccessfully wrote
[ { _id: 'Travel',
    name: 'Travel',
    title: 'Travelogues',
    description: 'This was great',
    date: Wed Jan 01 2014 00:00:00 GMT+1100 (AEDT) } ]
2. Sccessfully wrote
[ { _id: 'friends',
    name: 'friends',
    title: 'Friends',
    description: 'Random Pics',
    date: Thu Jan 02 2014 00:00:00 GMT+1100 (AEDT) } ]

当然,根据实际时区的不同,您要使用字符串输出。 另请注意, new Date("2014/01/01")new Date("2014-01-01")形式不相等。 第一个将日期构造为UTC时区对象,第二个将在本地时区中构造。 MongoDB将存储UTC,因此最好确保以这种方式对您存储的任何数据进行构造。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM