Null collection with mongodb driver for nodejs - node.js

I have shifted from mongoose to mongodb driver 3.1.13 for nodejs. My db resides on mlab. My problem is I am not even able to create a collection. Collection returned is null. Am I doing it the wrong way? Is there anything like save() function as we do have in mongoose.
image for the code is here

Related

Working with mongoose and Mongo DB schema

We are working with mongoose in order to interact with Mongo DB.
We define the schema at the code level.
Now we want to move and use Mongo DB schema (At the collection level inside DB) and we dont want to maintain two types of schemas.
We wish to keep on working with mongoose because it comes with few nice features that we like.
Any idea?

Is schema/model a must for reading data from mongodb database

Do we need a model/schema to just read data from a mongodb database.
I have this nodejs application with data already stored in mongodb. But the structure of the documents can change and schema diesnt allow for change.
So is it possible to read data without using a schema in mongoose or should I use the nodejs mongodb driver for this.
Thanks
Mongoose allows you to access the connection/db object, which will allow you to access your collections. You can then execute queries directly on a specific collection, without having to deal with models/schemas:
let yourCollection = mongoose.connection.db.collection('<tbd>');
const result = await yourCollection.find({...});

How to create MongoDB indexes in NodeJS app using Mongoose?

According to Mongoose documentation , the index should not be defined on the schema and should be disabled in production. I am confused as to what is being recommended. If index should not be defined in the mongoose schema then how it should be created in NodeJS app?

how to get all collection names from a specific db in mongo using nodejs

Recently I want to get all collection names from a db in mongo. And my develop environment is nodejs + express + mongoose. I tried mongooser.conncetion.db.getCollectionNames(or listCollections or collections), but any does not work. I guess the it is the version problem, maybe the latest mongoose api does not contain those functions.
Consequently, how to obtain all collection names from a db using mongoose?
Thank you so much for any help!

Is mongoose.Types.ObjectId() an absolutely Node.js client-side operation?

I'm using the Mongoose next to the Node.js. The question is:
Does the following command for creation of the ObjectID value make a call to the server?
mongoose.Types.ObjectId()
I've checked my local MongoDB server log, and it doesn't show anything like a call to the single local MongoDB node/server for requesting a new ObjectID value. However, I'm not sure if the default server log is about all the operations(trivial and essential ops), or not!
NOTE: trivial ops here means the non-manipulative data operations!
A very simple script suggests that it does not make a call to the server:
Installing mongoose by running npm install mongoose
Then make a 2-liner index.js:
var mongoose = require('mongoose');
console.log(new mongoose.Types.ObjectId);
Because we haven't even connected yet.

Resources