本文介绍了MongoDB的。插入了未定义的记录。无法读取undefined的属性'title'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MongoDB中的新手。一旦启动立即得到错误。
我有一个简单的代码

  var MongoClient = require('mongodb')。MongoClient; 

MongoClient.connect('mongodb:// localhost:27017 / mongotest',
函数(错误,db){
console.log('连接到MongoDB!') ;
//使用db连接对象,将集合'testing'保存到
//一个单独的变量:
var collection = db.collection('testing');
//使用集合的插入函数isert一个新项:
collection.insert({
'title':'Snowcrash'
},function(err,docs){
/ /成功插入后,在屏幕上记录新的
//集合的详细信息:
console.log(docs.length +'record inserted。');
console.log(docs [0 ] .title +' - '+ docs [0] ._ id);
//最后关闭连接:
db.close();
});
}) ;

这里是



请帮助我理解这个错误是什么以及如何解决它。如果你不复杂。



MongoDB版本3.0.7

解决方案

创建连接并插入MongoDB



我使用MongoDB 3.2.0和mongodb node.js驱动程序2.1.14



使用npm安装Node.js MongoDB驱动程序和BSON

  npm install mongodb bson 

和INSERT操作看起来像这样

  var mongodb = require('mongodb'); 
var bson = require('bson');

//创建连接
var server = new mongodb.Server('127.0.0.1','27017',{});
var client = new mongodb.Db('mydb',server,{w:1});

client.open(函数(错误){
if(error)
throw error;
//创建MongoDB集合
client.collection(' mongodb_insert',function(error,collection){
if(error)
throw error;
console.log('Connection!')
// CREATE / INSERT Operation
collection.insert(
{
key:value,
key1:value1
},
{safe:true},
函数(错误,文档)
{
if(error)
throw error;
console.log(document);
}

})
})


Im newbie in MongoDB. Once started immediately got the error.I have a simple code

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

MongoClient.connect('mongodb://localhost:27017/mongotest',
    function(err, db) {
        console.log('Connected to MongoDB!');
        // using the db connection object, save the collection 'testing' to
        // a separate variable:
        var collection = db.collection('testing');
        // isert a new item using the collection's insert function:
        collection.insert({
            'title': 'Snowcrash'
        }, function(err, docs) {
            // on successful insertion, log to the screen the new
            // collection's details:
            console.log(docs.length + ' record inserted.');
            console.log(docs[0].title + ' – ' + docs[0]._id);
            // finally close the connection:
            db.close();
        });
    });

and here is the error message

Help me please to understand what this error and how to fix it. If you do not complicate.

MongoDB version 3.0.7

解决方案

Create connection and insert to MongoDB

I use MongoDB 3.2.0 and mongodb node.js driver 2.1.14

Install Node.js MongoDB driver and BSON with npm

npm install mongodb bson

and INSERT operation looks like this

var mongodb = require('mongodb');
var bson = require('bson');

// Create Connection
var server = new mongodb.Server('127.0.0.1', '27017', {});
var client = new mongodb.Db('mydb', server, {w:1});

client.open(function(error){
    if(error)
        throw error;
    // Create MongoDB Collection
    client.collection('mongodb_insert', function(error, collection) {
        if(error)
            throw error;
        console.log('Connection!')
        // CREATE/INSERT Operation
        collection.insert(
            {
                "key": "value",
                "key1": "value1"
            },
            {safe: true},
            function(error, document)
            {
                if(error)
                    throw error;
                console.log(document);
            }
        )
    })
})

这篇关于MongoDB的。插入了未定义的记录。无法读取undefined的属性'title'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 05:48