How to get size of mongodb single document in bytes using nodejs? - node.js

I'm fetching a document using db.coll.findOne. I wanted to get size of the document in bytes using nodejs with only mongo-native driver.can it be possible?

It is possible using BSON (it's a dependency of the mongodb driver):
var bson = require("bson");
var BSON = new bson.BSONPure.BSON();
db.coll.findOne(query).then(function(err, doc) {
var size = BSON.calculateObjectSize(doc);
});

Related

Mongoose read without schema

Is there a way to read from MongoDB using Mongoose(for node.js) without defining a schema.
If I only want to print out all the data stored in a collection, like how the terminal command db.collectionName.find() works. Can I not pass a schema to achieve that?
Mongoose expose the mongodb.DB instance
via mongoose.connection.db, so you can use directly the
mongodb native driver
For example, if you want to print out all the data stored in a collection without
defining schema
let mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/testDB').then(() => {
const db = mongoose.connection.db;
db.collection('collection-name').find().toArray((err, result) => {
console.log(result)
});
}).catch(err => console.log(err.message))
See mongodb native driver documentation
for more examples

how to upload multiple files inside mongodb using nodejs

Hello friends I am new in nodejs and mongodb. Is it possible to upload multiple files inside single mongodb doc with other information.
You can make use of BSON (Binary JSON) to store files in MongoDB collections. However, BSON has a limit of 16MB. If you are planning to store files bigger than that, consider GridFS
You can write files to MongoDB like so in Node.js:
var Binary = require(‘mongodb’).Binary;
//Read the file that you want to store
var file_data = fs.readFileSync(file_path);
var db_doc = {};
db_doc.file_data= Binary(file_data);
var my_collection = db.collection(‘files’);
my_collection.insert(db_doc, function(err, result){
//more code..
....

Can't find a record even when using mongodb's ObjectID in sails.js

On sailsjs and using mongodb's ObjectID object, I can't findOne() a record in the database with the native mongodb driver.
// Included the ObjectID from the mongodb dependency on sails-mongo layer
var mongodb = require("sails-mongo").mongodb;
var ObjectID = mongodb.ObjectID;
// Inside the action2 controller...
var db = sails.getDatastore().manager;
var record = db.collection('event').findOne({_id: new ObjectID(inputs.event.id)});
// Returns an empty object
exits.success(record);
I expect to receive a json for the record in the database but I'm receiving an empty object.
It might be because you aren't getting the value of inputs.event.id or you are getting it as other data type and its not generating mongoDB objectId.
Log it and check.

Streaming a large CSV file into a mongoDB database using mongoose

Searching for an efficient and quick way to stream a large (10 million lines) of csv data into a mongoose database.
Problems that arise are dealing with streaming instead of importing which could be solve with fs.createReadStream (although still learning how to use it) and how to deal with inserting that large amount of data into the mongoDB using mongoose because overloading mongoose/mongo with insert requests could lead to some errors.
you simply need 'stream-to-mongo-db' and 'csvtojson' npm library.
here is the example code i use to dump millions of records from BIG csv files. It just works!
const fs = require('fs');
const csv = require('csvtojson');
const streamToMongoDB = require('stream-to-mongo-db').streamToMongoDB;
const csvFile = './bigFiles.csv';
const dbURL = 'mongodb://localhost/tweets';
const collection = 'tweets';
fs.createReadStream(csvFile)
.pipe(csv())
.pipe(streamToMongoDB({
dbURL : dbURL, collection : collection
}))
there is a insertMany() method in mongoose. but that only lets you insert 10 000 docs only per once so.. My solution is to you loop asyncly using that method and insert untll the stream finishes..

import data from a csv file into mongoDB collection with meteor

i would like to get data from a csv file into a mongoDB collection using Meteor js and i would be grateful for any help
You can use papa-parse and read csv file using Node file system like this:
var fs = Npm.require('fs');
// Assume that the csv file is in yourApp/public/data folder
var data = fs.readFileSync(process.env.PWD + '/public/data/yourCSV.csv', 'utf8');
var usersData = Papa.parse(data, {header: true});
The userData will in the JSON format, you can store it in the MongoDb as you want.
csv-parse is used to parse csv files. Loading the MongoDB collection can be done via the upsert method of Meteor collection.

Resources