How to add binary data with objectId to mongoDB? - javascript

I need to insert a document into a collection, which has an ObjectId and a BinData value. Therefore I don't know how to insert it.
With this code I get the error TypeError: Cannot read property 'ObjectId' of undefined.
server/fixtures.js
var ObjectId = Mongo.ObjectID;
var chunk = {
"_id" : ObjectId("57a9be3c89c1e4b50c574e3a"),
"files_id": ObjectId("5113b0062be53b231f9dbc11"),
"n" : 0,
"data" : BinData(0, "/9j/4AAQSkZJRgA...and...so...on../2Q==")
};
db.mediafiles.chunks.insert(chunk);
Update
I'm using Meteor
Therefore I can use var ObjectId = Meteor.Collection.ObjectID;. But how do I get the BinData?
ReferenceError: BinData is not defined

Stumbled upon this today as well.
As the other answer mentioned you can use ObjectID and Binary provided by the MongoDB driver. The issue I had was that the binary data was not what I expected after inserting and this is due to the inner workings of the Binary function. It requires either an unencoded string or a buffer, which can be initialized from base64 encoded content like this:
const { Binary, ObjectID } = require('mongodb')
async function run() {
// Configure MongoDB connection
const client = new MongoClient()
// Connect to MongoDB
await client.connect(...)
try {
// Insert data using base64 encoded content and
// both ObjectID and Binary from mongodb package
await client.db().mediafiles.chunks.insert({
_id: ObjectID('57a9be3c89c1e4b50c574e3a'),
files_id: ObjectID('5113b0062be53b231f9dbc11'),
n: 0,
data: Binary(Buffer.from('/9j/4AAQSkZJRgA...and...so...on../2Q==', 'base64')),
})
} finally {
// Close client if it was opened
await client.close()
}
}

Here is the NodeJS code to insert data into collection. To answer your question specifically, you need the below statement if you are using NodeJS.
var ObjectId = require('mongodb').ObjectID;
Full NodeJS code (assuming you are using NodeJS):-
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var ObjectId = require('mongodb').ObjectID;
var bindata = new require('mongodb').Binary("ZzEudm1s");
var insertDocument = function(db, callback) {
var chunk = {
"_id" : new ObjectId("535e1b88e421ad3a443742e7"),
"files_id" : new ObjectId("5113b0062be53b231f9dbc11"),
"n" : 0,
"data" : bindata
};
db.collection('Day1').insertOne(chunk, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the collection.");
callback();
});
};
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
db.close();
});
});
If you need a pure JavaScript object of ObjectId, you can use the below library.
https://www.npmjs.com/package/objectid-purejs

Related

How to use MongooseJS's findOne() and get an array of results?

I am new to Mongoose and I can't figure out how to get the results of my database from mlab.
I know how to findOneAndUpdate, but I am having trouble getting the results. The Mongoose site, I feel is not as informative as it could have been in terms of explaining how to get a particular result.
My MongoDB database looks something like this. The name of the collection is TagstoFiles.
I want to use Mongoose to put return all file_id's for a given tag.
I could not understand how to use findOne and findById from mongoose's site.
I tried writing some code but to no avail.
router.post('/decrypt_download', ensureAuthenticated, function(req, res){
var tags = req.body.ea;
var arrayOfFiles = [];
TagstoFiles.findOne({tag:tags[0]},function(err, file_id){
var obj = JSON.parse(file_id);
});
//how to use ^
res.send("Working.");
});
Can someone please explain how to accomplish this?
Here i explain how to use findOne and findById find usage its the same , to do what i understand you want you need to use find
var Wanted= require('../models/wanted');
Wanted.findOne({"idsearch":idofwantedele, "secondparam":otherfilter}).exec((err,result) =>{
if (err) return res.status(500).send({message : "something for an error"});
if (result ) {
return res.status(200).send({message: "here the result has the object of type wanted"});
}
if (!result ){
return res.status(400).send({message : "nothing with those filters"});
}
});
findById
var Wanted= require('../models/wanted');
Wanted.findById(wantedid, (err, result) => {
if(err) return res.status(500).send({message : "something for an error"});
if(!result) return res.status(404).send({message : "nothing with those filters"});
if(result) return res.status(200).send({message:"result is a wanted with wantedid"});
});
Schema
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var WantedSchema = Schema({
some: String,
secondparam:String,
reftootherschema: { type: Schema.ObjectId, ref: 'User' },
});
module.exports = mongoose.model('Wanted',WantedSchema);

MongoDB with Node Index Error

I'm currently using a node.js application to scan Twitter's API based on a set of parameters, and then uploading those JSON objects to a MongoDB database kept on MLab. I have connected to the database without issue, but my code will only upload ONE tweet before crashing. Here is the error message:
(node:62948) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): BulkWriteError: E11000 duplicate key error index: test-database.test-collection.$_id_ dup key: { : ObjectId('5aecb49e205197f5e4f52e32') }
It seems to have something to do with the keys that I am using in the database? How can I write my code so that I don't have this issue. Here is my program right now:
var Twitter = require("twitter");
var config = require("./config");
const mongoose = require("mongoose");
const MongoClient = require("mongodb");
var twitterClient = new Twitter(config);
const assert = require("assert");
const dbName = "test-database";
const collectionName = "test-collection";
const url = "mongodb://user:password#ds113870.mlab.com:13870/test-database";
const param = {follow: '21111098,958191744683782144,18061669,21111098,18061669,2891210047,1869975300,19394188,4107251,16056306,259459455,21111098,18061669,2891210047,1869975300,19394188,4107251,16056306,259459455,968650362,343041182,5558312,111671288,476256944,378631423,803694179079458816,30354991,224285242,45645232,235217558,20879626,150078976,278124059,102477372,249787913,381577682,15324851,435500714,823302838524739584,20597460,555355209,15745368,229966028,3001665106,2863210809,1397501864,78403308,253252536,47747074,1262099252,1284467173,92186819,169198625,600463589,413160266,1096059529,1095504170,1058520120,328679423,247334603,308794407,216503958,234128524,59969802,10615232,118740781,1383059977,2856787757,75364211,586730005,18632666,18632809,1249982359,339822881,365530059,216881337,3229124078,55677432,816683274076614656,26594419,1068481578,1068540380,19726613,13529632,18137749,3067974778,109071031,278094476,21406834,1129029661,970207298,357606935,236511574,145292853,76456274,456137574,33537967,941000686275387392,555474658,264219447,11650762,16160352,57065141,753693622692970497,21269970,238177562,389554914,11651202,214767677,515822213,16473577,1071402577,323490669,1480852568,2962923040,2987970190,811313565760163844,3145735852,266133081,41363507,109287731,14125897,946549322,361569788,15808765,1603426344,18695134,407039290,1099199839,183062944,60828944,325231436,14140370,17494010,1872999342,72198806,709389393811927041,21157904,213339899,2964174789,22195441,1061029050,460376288,382791093,106733567,43910797,24768753,18915145,240790556,2612307559,7270292,20546536,225921757,27044466,250188760,292495654,122124607,29201047,223166587,171598736,94154021,221162525,26062385,486694111,242555999,770121222,14845376,432895323,3219708271,217543151,81191343,2955485182,978029858,296361085,26533227,76649729,21669223,283130017,73303753,13218102,1648117711,1074480192,23022687,262756641,18170310,88784440,242836537,946946130,172858784,7429102,409719505,293131808,158470209,117501995,35567751,193794406,158890005,234374703,113355380,1074518754,87510313,233737858,291756142,1848942470,202206694,499268312'};
let newTweet = {
name: "",
text: "",
followers: ""
}
MongoClient.connect(url, function(err, client){
assert.equal(null,err);
console.log("connected.");
const db = client.db(dbName);
const collection = db.collection(collectionName);
const insertDocument = function(db, callback){
// THIS IS WHERE I THINK THE PROBLEM IS //
collection.insert(newTweet)
}
twitterClient.stream('statuses/filter',param,function(stream) {
stream.on('data', function(tweet) {
newTweet.name = tweet.user.screen_name;
newTweet.followers = tweet.user.followers_count;
newTweet.text = (tweet.extended_tweet) ? tweet.extended_tweet.text : tweet.text;
insertDocument(newTweet, function(){
db.close();
});
});
});
});
You are closing the database immediately after inserting one tweet.
// stream.on('data', function(tweet) {
insertDocument(newTweet, function(){
db.close();
});
Instead, close the connection on stream end.
stream.on('end', function() {
db.close();
});
You are getting the Duplicate key issue, because you declared the newTweet object globally, which shares the same object for every tweet. Declare the tweet object inside the stream.on('data') handler function. i.e.
stream.on('data', function(tweet) {
let newTweet = {};
newTweet.name = tweet.user.screen_name;
newTweet.followers = tweet.user.followers_count;

save method in Mongoose failed

I tried to use save() in Mongoose but saw empty collection been inserted.
my photo schema is like this :
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var photosSchema = mongoose.Schema({
photos: {
type: String
},
caption: {
type: String
}
});
var Photos = module.exports = mongoose.model('photos', photosSchema);
module.exports.getAllPhotos = function(callback){
Photos.findAll(callback);
}
and in my route I do
var Photo = require('../models/photos');
router.post('/upload_photo', upload.any(), function(req, res, next) {
var photo = new Photo();
var data = {
photos:'abc.jpg',
caption:'something..'
}
photo.save(data);
res.end();
});
Am I using save wrong here? I know I can define a method like savePhoto and export it, but how to use save() directly instead?
Try it like so, i.e. pass the data directly to the constructor and end the response stream when the async. save finished (and maybe add error handling as well).
var photo = new Photo({
photos:'abc.jpg',
caption:'something..'
});
photo.save(function(err) {
res.end();
});
There is also a shorthand create method on the model as described in the Mongoose docs
Try like this, and you will be able to see the issue if there's any:
var data = {
photos:'abc.jpg',
caption:'something..'
};
var photo = new Photo(data);
photo.save(function (err) {
if (err) console.log(err);
res.end();
});

How to get Mongoose to list all documents in the collection? To tell if the collection is empty?

I'm using a MEAN stack and writing these methods in Mongoose. I'm wondering what's wrong with what I put in the Mongoose model file. I would like to use Mongoose to simply print out a list all the documents in the myModel collection.
myModel.methods.myMethod = function(cb){
this.model("Bids").find({}, 'myField', function(err, results){
if (err){console.log(err);return err;}
console.log("okay");
console.log(results);
})
this.save(cb);
}
Also, what is the code that I can write in Mongoose to tell if the myModel collection is empty or not?
It's better to teach a man how to fish than to give him a fish ...
So it would be extremely helpful if you can suggest what debugging tools I can install, such as an Express middleware, that can help me debug myself. Please post your debugging suggestions here.
I'm assuming every other setup required for mongoose is correct.
At the line below, I think 'myField' is not needed.
this.model("Bids").find({}, 'myField', function(err, results)
Here is something more from scratch, maybe it would help you to trace-back you steps:
var mongoose = require('mongoose');
//connection to Mongodb instance running on=======
//local machine or anywhere=========================
var uri = 'mongodb://localhost:27017/test';
var connection = mongoose.createConnection(uri);
//Define Schema==================================
var Schema = mongoose.Schema;
var BlogPostSchema = new Schema({
author: { type: Schema.Types.ObjectId },
title: String,
body: String
});
//Create model===================================================
var BlogPostModel = connection.model('BlogPost', BlogPostSchema);
//function to insert doc into model NOTE "pass in your =======
//callback or do away with it if you don't need one"=========
var insertBlogPost = function (doc, callback) {
//here is where or doc is converted to mongoose object
var newblogPost = new BlogPostModel(doc);
//save to db
newblogPost.save(function (err) {
assert.equal(null, err);
//invoke your call back if any
callback();
console.log("saved successfully");
});
};
//function to get all BlogPosts====================================
var getAllBlogPosts = function (callback) {
//mongoose get all docs. I think here answers your question directly
BlogPostModel.find(function (err, results) {
assert.equal(null, err);
//invoke callback with your mongoose returned result
callback(results);
});
};
//you can add as many functions as you need.
//Put all of your methods in a single object interface
//and expose this object using module.
var BlogPostManager = {
insertBlogPost: insertBlogPost,
getAllBlogPosts : getAllBlogPosts
}
module.exports = BlogPostManager;

What is the correct way of passing a Mongoose object into the MongoDB underlying connection insert() method

I need to insert many thousands of documents into MongoDB. I want to use Mongoose for its casting properties, etc. However I cannot figure out how to pass the generated instances to the MongoDB connection. I have tried this:
var fs = require('fs');
var mongoose = require('mongoose');
var config = JSON.parse(fs.readFileSync("./config.json"));
mongoose.connect(config.mongoDBUrl);
db = mongoose.connection;
db.once('open', function () {
var TestSchema = new mongoose.Schema({
testStr : String
});
var Model = mongoose.model('test_schema_2', TestSchema);
var inst = new Model();
inst.testStr = "EWAFWEFAW";
// This works.
db.collection('test_schema_2').insert({ testStr : 'My Test Str'}, {}, function (err) {
if (err) {
console.log(err);
} else {
console.log('Written.');
db.close();
}
});
// This doesn't.
db.collection('test_schema_2').insert(inst, {}, function (err) {
if (err) {
console.log(err);
} else {
console.log('Written.');
db.close();
}
});
});
In the second case, I get: "[RangeError: Maximum call stack size exceeded]"
What is Mongoose breaking behind the scenes that stops this from working, and how can I make it work?
To save an instance of a model you just have to do
inst.save(function(err) {
//Do something here
});

Categories

Resources