Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MongoDB。插入未定义的记录。无法读取属性';标题';未定义的_Javascript_Mongodb_Database - Fatal编程技术网

Javascript MongoDB。插入未定义的记录。无法读取属性';标题';未定义的

Javascript MongoDB。插入未定义的记录。无法读取属性';标题';未定义的,javascript,mongodb,database,Javascript,Mongodb,Database,我是MongoDB的新手。一旦启动,立即得到错误。 我有一个简单的代码 var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/mongotest', function(err, db) { console.log('Connected to MongoDB!'); // using the db connection

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

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();
        });
    });
这里是

请帮助我了解此错误以及如何修复它。如果你不把事情复杂化


MongoDB版本3.0.7

我使用了模块
MongoDB 2.0.49
数据库mongodb3.0

插入到测试集合后,单据不是测试集合的单据

单据是查询的结果

看起来像

{ result: { ok: 1, n: 1 },
  ops: [ { 'it's you query'} ],
  insertedCount: 1,
  insertedIds: [ 'blah blah blah' ] }
因此,docs不是数组,而是json


这就是为什么文档[0]是未定义的(“文档不是数组!!”)

我使用了模块
mongodb 2.0.49
数据库mongodb3.0

插入到测试集合后,单据不是测试集合的单据

单据是查询的结果

看起来像

{ result: { ok: 1, n: 1 },
  ops: [ { 'it's you query'} ],
  insertedCount: 1,
  insertedIds: [ 'blah blah blah' ] }
因此,docs不是数组,而是json


这就是为什么文档[0]未定义(“文档不是数组!!!!”)

创建连接并插入到MongoDB

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

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

npm install mongodb bson
插入操作如下所示

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

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

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

npm install mongodb bson
插入操作如下所示

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

您应该用以下代码替换以前的代码

console.log(docs.ops.length + ' record inserted.');
console.log(docs.ops[0].title + ' – ' + docs.ops[0]._id);

您应该用以下代码替换以前的代码

console.log(docs.ops.length + ' record inserted.');
console.log(docs.ops[0].title + ' – ' + docs.ops[0]._id);

目前它看起来像是一个集合。插入({'title':'Snowcrash'}它是一个对象。MongoDB可以记录对象?但目前由于某种原因他没有插入。你的意思是查询不起作用吗?删除“console.log(docs.length+'record inserted.”;console.log(docs[0].title+'-+docs[0]。\u id);”!然后它就可以工作了!现在它看起来像这个
集合。insert({'title':'Snowcrash'}
它是一个对象。MongoDB可以记录对象吗?但是现在由于某种原因他没有插入。你的意思是查询不起作用吗?删除“console.log(docs.length+'record inserted');console.log(docs[0].title+'-+docs[0])。\u id)!