Could someone give this a try and see if it’ll work for them? You’d need Node/NPM and MongoDB at v3.x
At the moment, it’s giving the following error, but I’m not seeing why:
##Console Error Message
➜ mongodb-script-project node mongodb-script.js
Connection is okay
/Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:797
throw err;
^
TypeError: db.collection is not a function
at insertDocuments (/Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/mongodb-script.js:3:25)
at MongoClient.connect (/Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/mongodb-script.js:63:3)
at args.push (/Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/node_modules/mongodb/lib/utils.js:404:72)
at /Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:255:5
at connectCallback (/Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:933:5)
at /Users/christopherallanperry/development/edx_node/module_03/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
##mongodb-script.js
const insertDocuments = (db, callback) => {
// Get reference to edx-course-docs collection
const collection = db.collection('edx-course-students')
// Insert 3 documents
collection.insert([
{name : 'Bob'}, {name : 'John'}, {name : 'Peter'} // 3 documents
], (error, result) => {
if (error) return process.exit(1)
console.log(result.result.n) // will be 3
console.log(result.ops.length) // will be 3
console.log('Inserted 3 documents into the edx-course-students collection')
callback(result)
})
}
const updateDocument = (db, callback) => {
// Get the edx-course-students collection
var collection = db.collection('edx-course-students')
// Update document where a is 2, set b equal to 1
const name = 'Peter'
collection.update({ name : name }, { $set: { grade : 'A' } }, (error, result) => {
if (error) return process.exit(1)
console.log(result.result.n) // will be 1
console.log(`Updated the student document where name = ${name}`)
callback(result)
})
}
const removeDocument = (db, callback) => {
// Get the documents collection
const collection = db.collection('edx-course-students')
// Insert some documents
const name = 'Bob'
collection.remove({ name : name }, (error, result) => {
if (error) return process.exit(1)
console.log(result.result.n) // will be 1
console.log(`Removed the document where name = ${name}`)
callback(result)
})
}
const findDocuments = (db, callback) => {
// Get the documents collection
const collection = db.collection('edx-course-students')
// Find some documents
collection.find({}).toArray((error, docs) => {
if (error) return process.exit(1)
console.log(2, docs.length) // will be 2 because we removed one document
console.log(`Found the following documents:`)
console.dir(docs)
callback(docs)
})
}
const MongoClient = require('mongodb').MongoClient
// Connection URL
const url = 'mongodb://localhost:27017/edx-course-db'
// Use connect method to connect to the DB server
MongoClient.connect(url, (error, db) => {
if (error) return process.exit(1)
console.log('Connection is okay')
insertDocuments(db, () => {
console.log('insertDocuments() accessed')
updateDocument(db, () => {
removeDocument(db, () => {
findDocuments(db, () => {
db.close()
})
})
})
})
})
##package.json
{
"name": "mongodb-script",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^3.0.1"
}
}
The nearest I’ve found to an answer is in this SO thread - TypeError: db.collection is not a function
, but as far as I can see, I have my script set up correctly for a v3.x MongoDB environment, as per the response with ~59 up votes.