I have an array like [ 570dec75cf30bf4c09679deb, 56fe44836ce2226431f5388f ]. Now I want insert this for new collection named notifications like
{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("570dec75cf30bf4c09679deb")
}
and
{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
I have written a query in node.js to insert this but it inserting last element of array two times like this
{
"_id": ObjectId("57bd39c9734ff3602a312b33"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
and
{
"_id": ObjectId("57bd39c8734ff3602a312b32"),
"userId": ObjectId("56fe44836ce2226431f5388f ")
}
The query I have written like this
app.js
router.post('/creatList', function(req,res){
console.log(req.body.email);
var emails = req.body.email;
if(req.body.wData.wishListType == 'Shared'){
var findUsers = function(db, callback) {
var cursor;
cursor = db.collection('users').find({email: { $in: emails }})
cursor.toArray(function(err, docs){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs);
}
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
findUsers(db, function(err,docs) {
db.close();
console.log(docs);
for(var key in docs){
console.log(key);
var ids = docs[key]._id;
console.log(ids);
var insertDocument = function(db, callback) {
db.collection('notifications').insert({
"userId" : ids,
},function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the notifications collection.");
callback();
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
db.close();
});
});
}
});
});
});
You basically need to remap your results from you users.find() so that they will match your schema for your notifications collection. This should work:
var docsToInsert = docs.map(function (doc) {
// this assumes your users collection has an _id that you actually want to use as the reference for you notifications.userId field
// actually, you probably want "return {"userId": ObjectID(doc._id)};" since you are using the native mongodb api
return { "userId": doc._id };
});
So, simplifying what you have for the rest a bit, it would be:
router.post('/creatList', function (req, res) {
console.log(req.body.email);
var emails = req.body.email;
if (req.body.wData.wishListType == 'Shared') {
var findUsers = function (db, callback) {
var cursor;
cursor = db.collection('users').find({ email: { $in: emails } });
cursor.toArray(function (err, docs) {
if (err) {
callback(new Error("Some problem"));
} else {
callback(null, docs);
}
});
};
MongoClient.connect(config.database, function (err, db) {
assert.equal(null, err);
findUsers(db, function (err, docs) {
db.close();
console.log(docs);
var docsToInsert = docs.map(function (doc) {
// this assumes your users collection has an _id that you actually want to use as the reference for you notifications.userId field
// actually, you probably want "return {"userId": ObjectID(doc._id)};" since you are using the native mongodb api
return { "userId": doc._id };
});
MongoClient.connect(config.database, function (err, db) {
assert.equal(null, err);
db.collection("notifications").insert(docsToInsert, function (err, result) {
if (err) {
// handle the err however you like
throw err;
}
db.close();
});
});
});
});
}
});
Related
app.get('/projects/:company_id', function(req, res){
var company_id = req.params.company_id;
//query1
db.projects.count({"company_id": company_id },function(err, doc){
res.json(doc);
});
//query2
db.projects.find({"company_id": company_id },function(err, doc){
res.json(return_data);
});
});
Basically I want these two queries to run in parallel and then send the output to front end. I am not sure how to achieve that, guys help!.
Also is there an easy way to separate these two results in front end. I am using angular js.
You want to use Promise.all() to wait both queries are finished.
Of course you need promises inside it, so we wrap each of the two function of yours in a promise, which is fulfilled when the callback is called.
If one of the two queries fail, then also Promise.all() fails and we send back to the client the error.
To separate data in the reply I split the data in two fields, count and find. In this way you can read them easily from angularjs and use them as you wish
app.get('/projects/:company_id', function(req, res){
var company_id = req.params.company_id;
Promise.all(
new Promise(resolve, reject) {
db.projects.count({"company_id": company_id },function(err, doc){
if (err) return reject(err);
resolve(doc);
});
},
new Promise(resolve, reject) {
db.projects.find({"company_id": company_id },function(err, doc){
if (err) return reject(err);
resolve(doc);
});
}
)
.then(data) {
res.json({
"count": data[0],
"find": data[1]
});
}
.catch(error) {
res.json({
"error": error
});
}
});
If you can't use Promises 'cause you're using node.js 0.10.x, you can do something like this:
app.get('/projects/:company_id', function(req, res){
var company_id = req.params.company_id;
var reply = {
find: -1,
count: -1
}
var errorSent = false;
function sendErr(e) {
if (errorSent) return; // Do not send twice a reply
res.json({"error": e});
errorSent = 1;
}
function sendWhenReady() {
if (reply.count !== -1 && reply.find !== -1) res.send(reply);
}
//query1
db.projects.count({"company_id": company_id },function(err, doc){
if (err) return sendError(err);
reply.count = doc;
sendWhenReady();
});
//query2
db.projects.find({"company_id": company_id },function(err, doc){
if (err) return sendError(err);
reply.find = doc;
sendWhenReady();
});
});
You could run them side by side and send the data after both run. Check async library which is one of many libraries to do such operations easily.
var async = require("async");
async.parallel({
one: function(callback) {
db.projects.count({"company_id": company_id },function(err, doc){
callback(null, doc)
});
},
two: function(callback) {
db.projects.find({"company_id": company_id },function(err, doc){
callback(null, doc);
});
}
}, function(err, results) {
var payload = {
count : results.one,
data: results.two
}
res.json(payload);
});
If you want to do it without any additional library, you could do something similar to this:
app.get('/projects/:company_id', function(req, res) {
// Create an object resembling your final response,
// but replace the values with a promise.
var futureData = {
count: new Promise(function(resolve, reject) {
db.projects.count({'company_id': company_id}, function(err, doc) {
if (err) {
return reject(err);
}
return resolve(doc);
});
}),
results: new Promise(function(resolve, reject) {
db.projects.find({'company_id': company_id}, function(err, doc) {
if (err) {
return reject(err);
}
return resolve(doc);
});
}),
};
var keys = Object.keys(futureData);
// Wait until all promises are resolved.
Promise.all(
keys.map(key => futureData[key])
)
// Get the response object.
.then(function(values) {
// Build an object from the results of the promises,
// using the same key as in `futureData`.
return values.reduce(function(data, value, i) {
var key = keys[i];
data[key] = value;
return data;
}, {});
})
.then(function(data) {
// Send final data.
res.json(data);
})
.catch(function(err) {
// TODO: Handle error.
});
});
i want to get the object from my collection based on the quesListName which i send as a param to the server
here is my service
angular.module('hrPortalApp')
.service('getCandidateInterviewListService', function($http, ajaxServiceManager) {
var sUrlQuestions = "http://localhost:4000/onboardvue/questions/qListQuestions/";
return {
fnGetQuestions: function(qListName) {
return ajaxServiceManager.fnQuery({
sUrl: sUrlQuestions,
sMethod: "GET",
oData: null,
oParams: {
quesListName: qListName
}
});
},
};
});
below is my schema
var QuestionsSchema = new Schema({
topicName: String,
quesListName: String,
question:String
});
and the query which i wrote to get the object based on quesListName is
exports.query = function(req, res) {
Questions.find({quesListName:req.query.quesListName}, function(err, questions) {
if (err) {
return handleError(res, err);
}
return res.status(200).json(fnData(questions));
});
};
but i am getting 500 error
Right now i am writing data to json file and getting that back to html page to display. Now i want to do same with mongodb Database. I have tried something but, it doesn't working.
app.get('/', function(req, res){
url = 'http://www.amazon.in/Sony-Xperia-Z3-Copper-32GB/dp/B010V448ZC/ref=pd_rhf_se_s_cp_6?ie=UTF8&dpID=419rmomR%2BjL&dpSrc=sims&preST=_SL500_SR135%2C135_&refRID=19RT23W7T48Z99XNT6GK';
request(url, function(error, response, html){
if (!error) {
var $ = cheerio.load(html)
var json = {Product_Name : "", Brand : "", Color : "", Image : "", Price : "", Rating : ""};
var P_name = $('#title').children().text();
var brand = $('#brand').text();
var color = $('.a-row').find('span.selection').text();
var price = $('#price').find('span.a-size-medium').text();
var rating = $('#averageCustomerReviews').find('span.a-icon-alt').text();
var image = $('.imgTagWrapper').children().attr('src');
/*json.Product_Name = P_name;
json.Brand = brand.trim();
json.Color = color.trim();
json.Price = price.trim();
json.Rating = rating.trim();
json.Image = image.trim();
fs.writeFile('output.json', JSON.stringify(json, null, 4), function(err){
console.log('File successfully written! - Check your project directory for the output.json file');
})*/
var insertDocument = function(db, callback) {
db.collection('proInfo').insertOne( {
"Product_Name": P_name,
"Brand":brand,
"Color":color,
"Price":price,
"Rating":rating,
"Image":image
}, function(err, result) {
assert.equal(err, null);
console.log("Inserted a document into the proInfo collection.");
callback(result);
});
};
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
insertDocument(db, function() {
db.close();
});
});
res.send('Check your console!')
} else {
console.log("We’ve encountered an error: " + error);
}
})
})
It shows some error in console.log
D:\Hemanth\Node Js\web scraper\node_modules\mongodb\lib\url_parser.js:20
throw new Error('invalid schema, expected mongodb');
^
Error: invalid schema, expected mongodb
at module.exports
Can anybody help me to fix this issue?
Use module export in your node js routes and define the method inside the module.export as mentioned below :
module.export {}
and then call the method, apply routes in the server.js under node application :
erc(app,
{
controllers: __dirname + '/routes',
routes:{'/methodnametobecalled': { action: 'routesfilename#methodname'}
}
});
Initialize Mongoose and Schema
var mongoose = require('mongoose');
mongoose.connect('mongodb://YOURVALUES.mongolab.com:11111/NAME');
var schema = new mongoose.Schema({ name: 'string', account: 'string', date: 'string' });
var accountz = mongoose.model('accountz', schema);
Create
var small = new accountz({
name: "SAMZ",
account: "Monthly",
date: "29/12/2015"
});
small.save(function (err) {
if (err){
console.log("Error in Save");
}else{
console.log("Save Sucessfully");
}
});
Read
accountz.find().exec(function(err, data){
if (err){
console.log("Error in Reading");
}else{
console.log("The value = " + data);
}
});
Update
accountz.findOne({ "_id": "0023"}, function (err, doc){
doc.name = editObj.name;
doc.account = editObj.account;
doc.date = editObj.date;
doc.save(function (err) {
if (err){
console.log("Error in Updating");
}else{
console.log("Updated Sucessfully");
}
});
});
Delete
accountz.remove({ "_id": "0023"}).exec(function(err, data){
if (err){
console.log("Error in Deleting");
}else{
console.log("Deleting Sucessfully");
}
});
Ref This link https://shiyamexperience.wordpress.com/2015/12/29/mongodb-crud-using-mongoose/
I have this controller which fetch data from instagram api.
controller.prototype.getData = function getData(url, tag, callback) {
var clientId = '81e3d3f35c8a4438964001decaa5a31f'
var catchAll = [];
var config = {
'params': {
'client_id': clientId,
'callback': 'JSON_CALLBACK'
}
}
vm.url = 'https://api.instagram.com/v1/tags/' + tag + '/media/recent/';
http.jsonp(url, config)
.then(function(response) {
vm.imageData = vm.imageData.concat(response.data.data);
vm.instagram.storeData(vm.imageData).then(function(response) {
})
var paging = response.data.pagination;
if (paging.hasOwnProperty('next_url')) {
getData(paging.next_url)
} else {
callback.call();
}
})
}
then on node mongodb controller i have this
exports.create = function(req, res) {
var dataObj = req.body;
dataObj.forEach(function(item) {
Data.find({
'id': item.id
}, function(err, data) {
if (err) {
return handleError(res, err);
}
if (data.length == 0) {
Data.create(item, function(err, data) {
if (err) {
return handleError(res, err);
}
});
}
});
});
};
The logic here is that if user is not existing in the collection i will save it. my question is why is it that it takes too long to response if have lets say 300 instagram object ? is there a ways how to do this in optimal performance?
I have a bunch of tasks that when I create/edit I would like to be able to assign to a project. The way I have thought this should be done is the save the project ID to the task and the some how from the project collection reach into the task collection search for all objects with the matching project _id and then populate the tasks array with the correct tasks.
Now I don't know if this is the correct way and as you can see from my code I think I am half way there but I am not sure exactly how to populate a project with the correct tasks. I am using mongoose and it would be great if someone could let me know what I am doing wrong and how I could do it better.
I have a list of tasks that I want to link to projects.
TaskSchema:
var TaskSchema = new Schema({
title: { type: String},
description: String,
project: [{ type: Schema.Types.ObjectId, ref: 'Project'}]
});
Tast Controller:
'use strict';
var _ = require('lodash');
var Task = require('./task.model');
// Get list of tasks
exports.index = function (req, res) {
var populateUsers = {path:'users', select:'name profileImage email'};
Task
.find({})
.populate(populateUsers)
.populate('projects')
.exec(function (err, tasks) {
if (err) { //handle error
return handleError(res, err);
}
return res.json(200, tasks);
});
};
exports.show = function (req, res) {
var populateUsers = {path:'users', select:'name profileImage email'};
Task
.findById(req.params.id, function (err, task) { //get task
if (err) { //handle error
return handleError(res, err);
}
return task;
})
.populate(populateUsers)
.exec(function (err, task) { //use a callback to handle error or return tasks if everything is ok
if (err) { //handle error
return handleError(res, err);
}
return res.json(task); //return task filled with "users"
});
};
// Creates a new task in the DB.Î
exports.create = function(req, res) {
Task.create(req.body, function(err, task) {
if(err) { return handleError(res, err); }
return res.json(201, task);
});
};
// Updates an existing task in the DB.
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Task.findById(req.params.id, function (err, task) {
if (err) { return handleError(res, err); }
if(!task) { return res.send(404); }
var updated = _.merge(task, req.body);
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, task);
});
});
};
// Deletes a task from the DB.
exports.destroy = function(req, res) {
Task.findById(req.params.id, function (err, task) {
if(err) { return handleError(res, err); }
if(!task) { return res.send(404); }
task.remove(function(err) {
if(err) { return handleError(res, err); }
return res.send(204);
});
});
};
function handleError(res, err) {
return res.send(500, err);
}
ProjectSchema:
var ProjectSchema = new Schema({
name: String,
dateCreated : {
type : Date,
default : Date.now
},
tasks: [{ type: Schema.Types.ObjectId, ref: 'Task'}]
});
Get Project List
// Get list of projects
exports.index = function (req, res) {
Project
.find({})
.populate('tasks')
.exec(function (err, projects) {
if (err) { //handle error
return handleError(res, err);
}
return res.json(200, projects);
});
};