Mongoose 4 to 5: connection.db.collection() undefined - node.js

I'm attempting to upgrade from Mongoose 4.x to 5.x. So far the biggest issue I'm running in to is after the database has connected I'm unable to fetch collections like I was able to in 4.x.
const someCollection = mongoose.connection.db.collection('someCollection');
This code works fine in 4.x, but apparently the underlying structure of Mongoose has changed between versions.
Is there an equivalent way to do this in Mongoose 5.x? I looked in the documentation and the migration guide but didn't see anything.

For Mongoose 5.x, the .db part isn't needed so it should be:
mongoose.connection.collection('someCollection');

Related

How can i configure Mongoose to automatically create objects in my mongoDB on app start if none is found?

I am coming from a Java Hibernate frameworks, where an sql script can be written and added to a project, with the intentions of creating objects in my Mongo Database when the app runs and no data was found in a specific database table. I am using Mongoose presently with mongoDB, and i want to initialize my mongoDB table with objects if none is found. Initially i achieved this my creating an object and calling the new mongoose.save(obj) inside my app.js which is my start up file. But is there a cleaner way one already supported by Mongoose, or is this my best bet ?

nodejs mongodb driver and GridFS

I'm using nodejs and the native mongodb driver v2.0.43 (latest as of 9/18/2015) and am having problems querying the db.fs.files collection. It is returning that fs is undefined yet I can use the mongo console and see the files that have been stored using db.fs.files.find().toArray(). Does anyone have any experience querying the db.fs.files collection via node using the mongodb driver?
thanks
Ben
Ok, after doing a better google query I found this solution: Can you use find queries on GridFS using javascript API?
The gist is you have to use 'fs.files' as the collection name.
db.collection('fs.files').find().toArray();

Validating array items with joi in a Foxx app

I am having problems with using joi array-item validation in foxx applications as well as the arango-shell with arangodb-2.5.5.
The joi-documentation suggests to use something like:
var Joi = require('joi');
Joi.array().items({'name': Joi.string().required()});
for validating a dataset similar to:
[{'name': 'Peter'}, {'name': 'Edeltraut'}, ...]
However, using it in a Foxx application results in the application to stop working. Pasting the snippet from above into the arango-shell produces the following output:
JavaScript exception: TypeError: undefined is not a function
!Joi.array().items({'name': Joi.string().required()});
! ^
stacktrace: TypeError: undefined is not a function
at <shell command>:1:13
Is there something I am missing, or is arangodb using a modified / smaller version of joi that has this feature stripped out?
ArangoDB 2.5 uses an older version of joi. The method in question was renamed to items in joi 6.0 and was previously called includes.
You can find the documentation for joi 4.9.0 (the version shipped with ArangoDB 2.5) at https://github.com/hapijs/joi/tree/v4.9.0
ArangoDB 2.6 will ship with joi 6.4.3, which is the latest version at this time.
If you want to know the version of an NPM dependency shipped with ArangoDB, you can also find out the version number by importing its package.json file like this:
require('joi/package.json').version
You don't have to wait for ArangoDB 2.6 to use the latest version of joi. If you npm install joi --save inside of your Foxx app's APP folder, it'll be used instead of the one bundled with ArangoDB. I've been doing that for a while now.

Get Object has no method 'executeDbCommand'

I am using mongodb 3.0.2 with mongoose 4.0 in nodejs
While i upgrade mongoose 2.3 to 4.0 it started to throw error for
'executeDbCommand', it looks like this is deprecated in mongoose 4.0,
anyone has an idea for replacement of this command ?
I tried to dig into the mongoose but cant find it, Please help me if anyone knows !!
Mongoose uses a native driver in itself prototype, and api docs says that native drive has a command method. The code will be:
var db = mongoose.createConnection('mongodb://user:pass#localhost:port/database');
db.command('sample command');

Getting started with a database for Node

The most relevant question here on StackOverflow was this, which still doesn't answer my question because the answer nor the redis repo give a tutorial/walk-through that beginners don't understand.
The thing is, I have absolutely no idea how to setup a simple database in order to create a simple to-do list or blog on my own. This is probably the closest tutorial on how to setup a database. But it's lacking in a sense due to not having a schema or so defined in order for me to edit or add "tables".
Simply put, I'm looking for a tutorial a complete beginner is able to follow on how to setup a database and define custom schemas for data (e.g. products that can have reviews nested in them).
Any suggestions?
I guess this should cover it: Node.js, MongoDB and Mongoose
You mentioned that you wanted to create a Todo application. There is TodoMVC. You can see various MVC Frameworks in action. There's also an example with Mongoose and Backbone.
For installing different NoSQL options including MongoDB, CochDB, Redis and SQLite for use with Node.js this is a nice walk through.
Once installed the following steps are required to get going with the database. Typical example for mongodb:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/your_database');
var Schema = mongoose.Schema;
var User = new Schema({
'_id' : String,
'name' : String,
'votes' : Number });
var User_Model = mongoose.model('User', User);
Approaches to read/ write data from/to the database varies.
I think the simplest thing to do is to download and install MongoDB and use the mongodb-native driver to store and access your data.
MongoDB is schema-less so you won't need to define any table or keys in advance. Simply open a collection (which will be created automatically if it doesn't exist) and start storing documents/objects in it.
Mongo is fast, powerful and, in my opinion, easy to use.
See mongodb.org for more information.
I find this screencasts very helpful for nodeJS + MongoDB, even though the mongoose has been updated a lot since the video, but the basics remained. And you'll learn the new one in no time just by skimming mongoose's doc.
I've downloaded all of his videos and watch them every time I need brushing up my node skillz :p
Sidenote: He just uploaded screencast for couchDB, for other who prefers different cup of tea.

Resources