尝试insertMany时MongoDB和express,连接超时

时间:2022-06-15 12:25:57

I'm starting to learn node, express, and mongodb.

我开始学习node,express和mongodb。

In one of my getting started exercises I'm trying to do single and multiple inserts into a collection called documents.

在我的一个入门练习中,我正在尝试将单个和多个插入到称为文档的集合中。

Single inserts work fine:

单个插件工作正常:

let express = require('express')
let app = express()

let MongoClient = require('mongodb').MongoClient
let assert = require('assert')
let url = 'mongodb://localhost:27017/test'

app.get('/testInsert',(req,res) => {
    MongoClient.connect(url, (err, db) => {
        assert.equal(null, err)
        console.log("Connected to mongo server")

        let collection = db.collection('documents')

        // this works fine
        collection.insertOne({test: 'worked'}, (err, rec) => {
            assert.equal(null, err)
            assert.equal(1, rec.insertedCount)
            console.log('single insert result: ' + rec)
            db.close();
            res.send('single insert result: ' + rec)
        })

    })
})

But when I try to insert many I get a timeout:

但是当我尝试插入很多时,我会得到一个超时:

let express = require('express')
let app = express()

let MongoClient = require('mongodb').MongoClient
let assert = require('assert')
let url = 'mongodb://localhost:27017/test'

app.get('/testInsert',(req,res) => {
    MongoClient.connect(url, (err, db) => {
        assert.equal(null, err)
        console.log("Connected to mongo server")

        let collection = db.collection('documents')

        // This fires
        console.log('about to start insert many')
        collection.insertMany([{greeting: 'hi'}, {greeting: 'hey'}, {greeting: 'yo'}]), (err, recs) => {
            // I never get past here
            console.log('finished insert many')
            assert.equal(err,null)
            assert.equal(3,recs.result.n)
            assert.equal(3,recs.insertedCount)
            db.close();
            res.send("multi insert result: " + recs)
        }
    })
})

When I try hitting the endpoint I get a request timeout: 尝试insertMany时MongoDB和express,连接超时

当我尝试点击端点时,我得到一个请求超时:

But when I check mongo the records are definitely inserted: 尝试insertMany时MongoDB和express,连接超时

但是当我检查mongo时,肯定会插入记录:

I'm assuming I'm doing something wrong with the mongo insert because I never get to the first line of my callback. Is there something that I'm doing wrong when trying to insert multiple documents into mongo?

我假设我在使用mongo插件时出错了,因为我从未进入回调的第一行。在尝试将多个文档插入mongo时,是否存在我做错的事情?

1 个解决方案

#1


0  

On this line in your code:

在您的代码中的这一行:

"collection.insertMany([{greeting: 'hi'}, {greeting: 'hey'}, {greeting: 'yo'}]), (err, recs) "

There shouldn't be a closing parens ) after the closing bracket ]. I think if you fix that it should help.

在结束括号之后不应该有一个关闭的parens]。我想如果你解决它应该有所帮助。

#1


0  

On this line in your code:

在您的代码中的这一行:

"collection.insertMany([{greeting: 'hi'}, {greeting: 'hey'}, {greeting: 'yo'}]), (err, recs) "

There shouldn't be a closing parens ) after the closing bracket ]. I think if you fix that it should help.

在结束括号之后不应该有一个关闭的parens]。我想如果你解决它应该有所帮助。