There are a number of questions that relate to this but none of the answers seem to fix my problem.
I read json object strings from a file line by line - convert them to json objects then use some of the json object to find whether they are in mongodb. If they are I want to update the isValid field to true but I'm just getting a return value of 1 and no update happens.
Here's my code:
var mongodb = require('mongodb')
, MongoClient = mongodb.MongoClient;
var lazy = require("lazy"),
fs = require("fs");
MongoClient.connect('mongodb://localhost', function(err, db) {
var collection = db.collection('offers');
new lazy(fs.createReadStream('./offers.txt'))
.lines
.forEach(function(line){
var jsonOffer = JSON.parse(line.toString());
var find = {
pricing: jsonOffer.pricing,
details: jsonOffer.details,
retailer: jsonOffer.retailer,
brand: jsonOffer.brand,
type: jsonOffer.type
}
collection.update(find, { $set: { 'isValid': true } }, function (err, result) {
if (err) throw err;
console.log(result);//prints 1
});
});
});
Can anyone help me here?
Thanks!
Related
var express = require("express")
let PersonModel = require('./PersonModel')
let mongodbConnected=require('./MongodbConnect')
var app =express()
var bodyparser=require("body-parser")
const { format } = require("path")
const { count } = require("console")
const { countDocuments } = require("./PersonModel")
const { exec } = require("child_process")
const { get } = require("http")
const { ALL } = require("dns")
app.use(bodyparser.urlencoded({extended:false}))
app.get('/',function(req,res){
res.sendFile('Person.html', { root: __dirname });
})
app.get('/about',function (req,res){
res.send("This is a simple express application using mongodb express html and mongoose")
PersonModel.countDocuments().exec()
.then(count=>{
console.log("Total documents Count before addition :", count)
}) .catch(err => {
console.error(err)
})
})
app.post('/add', function(req,res){
Pname=req.body.empname
console.log('Pname',Pname)
PAge=req.body.Age
PGender=req.body.gender
PSalary=req.body.salary
const doc1 = new PersonModel(
{
name:Pname,age:33,Gender:PGender,Salary
:PSalary}
)
doc1.save(function(err,doc){
if (err) return console.error(err)
else
console.log("doc is added ",doc)
//res.send("Record is added"+doc)
res.send({
'status':true,
'Status_Code':200,
'requested at': req.localtime,
'requrl':req.url,
'request Method':req.method,
'RecordAdded':doc});
}
)
})
app.post('/findperson', function(req,res){
PAge=req.body.Age
console.log("Page",PAge)
PersonModel.find({age:{$gte:PAge}})
// find all users
.sort({Salary: 1}) // sort ascending by firstName
.select('name Salary age')// Name and salary only
.limit(10) // limit to 10 items
.exec() // execute the query
.then(docs => {
console.log("Retrieving records ",docs)
res.send(docs)
})
.catch(err => {
console.error(err)})
})
app.post('/delete', function(req,res){
Pgender=req.body.gender
PersonModel.findOneAndDelete({Gender:Pgender }
).exec()
.then(docs=>{
console.log("Deleted")
console.log(docs); // Success
}).catch(function(error){
console.log(error); // Failure
});
})
app.post('/update', function(req,res){
Pname=req.body.empname
Pnewname=req.body.newname
PnewAge=req.body.newage
PersonModel.findOneAndUpdate({ name: Pname },{"$set":{name:Pnewname,age:PnewAge}}).exec()
.then(docs=>{
console.log("Update for what i get is ",Pname
,Pnewname,PnewAge)
console.log(docs); // Success
}).catch(function(error){
console.log(error); // Failure
});
})
var docnum=PersonModel.countDocuments(ALL)
app.post('/count', function(req, res){
res.send('Total number of documents: ', docnum)
})
app.listen(5000,function(){
console.log("Server is running on the port 5000")
})
Hello.
First time posting on stackoverflow, dont know what kind of information to post, please let me know.
Im trying to make a page (/count) to simply display the number of documents. I've tried different code but nothing is working. This error keeps coming up "TypeError: Converting circular structure to JSON".
This is school work so the code is given to me by a teacher and I have to add a POST method to add a page that displays total number of documents.
Any ideas?
Thanks.
Circular structure is not about mongo but how JS read the JSON object.
For example, if you have this object:
var object = {
propA: "propA",
propB: object
}
When JS try to deserialize JSON object, will handle that: One object contains the object that contain again the object and again and again... that is a circular dependence.
Not only with one object itself, aslo with more objects:
var objectA = {
propA: objectB
}
var objectB = {
propA: objectA
}
Is the same case.
Using node.js you can use util.inspecet() which automatically show [Circular] when a circular dependence is found.
You can use like this:
var util = require('util')
console.log(util.inspect(objectA))
Here is a simple express router, I want to give it query params and search the DB for them.
so, if the URL is like this api?x=value1 the app should query the DB for { x:value1 }
if the URL is api?x=value1&y=value2 the app should query the DB for { x:value1, y:value2 }
Since I don't know the number of params in advance, I have created an empty object and appended it with the params if existed.
if there are no params I want to retrieve all documents in DB.
.get(function (req, res){
let update_issue= {}; /*empty object*/
if(req.query.issue_title){update_issue["issue_title"] = req.query.issue_title}
if(req.query.issue_text){update_issue["issue_text"] = req.query.issue_text}
if(req.query.created_by){ update_issue["created_by"] = req.query.created_by }
/*append object if param exists*/
if(Object.keys(update_issue).length !== 0 ){ /*check if that object is not empty*/
db.collection('issues').find(update_issue, (err, data)=>{
res.json(data);
})
}
db.collection('issues').find().toArray((err, data)=>{
res.send(data);
})
this solution keeps giving me TypeError: Converting circular structure to JSON.
I understand that the object is in the form { x : "value" } and it should be JSON object like this { "x": "value" }
I tried flatted, JSON-stringify-safe still the same problem.
can you give me a solution to this problem, or an alternative way to continue the work.
I have solved the problem using node package called Api query params.
here is my code:
var aqp = require('api-query-params');
.get(function (req, res){
let update_issue= aqp(req.query);
if(Object.keys(update_issue).length !== 0 ){ /*check if that object is not empty*/
db.collection('issues').find(update_issue, (err, data)=>{
res.json(data);
})
}
db.collection('issues').find().toArray((err, data)=>{
res.send(data);
})
here is the package : https://www.npmjs.com/package/api-query-params
I have some data I want to write to a file periodically and I'd like to write it small JSON objects. At a later time I'd like to read them all in for processing, but the appended file of JSON objects isn't JSON itself.
So I stringify the JSON object and write them to file periodically and I get something like this;
{
"100": {
"stuff": 123
}
}
{
"300": {
"stuff": 456
}
}
{
"200": {
"stuff": 789
}
}
Of course when I try to parse the file with a simple script like the following;
var fs = require('fs');
var file = 'test.log'
var obj = JSON.parse(fs.readFileSync(file, 'utf8'));
var fs = require('fs');
var obj;
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
console.log(obj);
});
...the parser freaks out because this isn't a valid JSON file, I'd need something more like;
{
"100": {
"stuff": 123
},
"300": {
"stuff": 456
},
"200": {
"stuff": 789
}
}
...but I can't get this by simply appending records. I can of course force commas between the records before writing them, but I end up missing the '{' at the start of the file, the '}' at the end of the file and would have an extra ',' on the last record. The whole things reaks of being a kludge.
I'm guessing someone has worked all of this out alreadyand there is a nice pattern for this, but I couldn't find anything from searching. Each section will have a variable amount of data so I like the flexibility JSON offers, but I don't want to store it all ahead of time before writing to disk as it will get large.
If you could point me at a good solution for this it would be appreciated.
Sincerely, Paul.
Why don't you use a regex before processing the object list file to add a comma. Since the pattern is a new line after every object, you could use this to find and replace: /}\n{/g.
For your sample, this should do the trick :
var fs = require('fs');
var file = 'test.log'
var obj;
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
var jsonString = '[' + data.replace(/}\n{/g, '},{')+ ']'; // Adding brackets in order to create an array of objects
obj = JSON.parse(jsonString);
console.log(obj);
});
Then, if you want to have an object as you specified you can use the spread operator ... to append an object to your super-object :
var fs = require('fs');
var file = 'test.log'
var obj;
fs.readFile(file, 'utf8', function (err, data) {
if (err) throw err;
var jsonString = '[' + data.replace(/}\n{/g, '},{')+ ']'; // Adding brackets in order to creat an array of objects
obj = JSON.parse(jsonString);
var superObj = {};
for (var i = 0; i < obj.length; i++) {
superObj = {...superObj,...obj[i]};
}
console.log(superObj);
});
I am trying to format the JSON output through node js as shown below
[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interface","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}
But currently i am getting the output as shown below
[{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["100","test_bsc_interface","test_bsc_interface","test","Open Loop","regex","HPSA",null,null,"Asynchronous",null,"0"]},{"headers":["id","name","description","type","actionmode","outputparser","dispatchtype","troubleticketaction","alarmaction","actionexecutionmode","cost","isparent__"],"values":["101","check_tt","check_tt","test","Open Loop","None","Trouble Ticket","check_tt",null,"Synchronous",null,"0"]}
Please find code snippet that i have used : may know whats the changes required in the code ::
var json_string=JSON.stringify(rows,null,2);
var json_object=setValue(JSON.parse(json_string));
if (!err){
console.log('The object are returning ',json_object);
var result = _.map(json_object, function(o) {
return {headers: _.keys(o), values : _.values(o)}
});
Your problem seems to be from another post I answered:
How to format the JSON object key/value pair
var json_string=JSON.stringify(rows,null,2);
var json_object=setValue(JSON.parse(json_string));
var result;
if (!err){
console.log('The object are returning ',json_object);
if (_.isArray(json_object) && json_object.length > 0) {
result = {
headers: _.keys(json_object[0]),
values: _.map(json_object, function(o) {
return _.values(o);
})
};
}
}
I'm trying to get a JSON object returned from my mongoDB using mongoose (latest version). Everything works find but i get an empty array returned [].
app.js
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
dependencies
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
var express = require("express")
var cons = require("consolidate")
var app = express()
var db = require("./model/db")
var path = require("path")
var routes = require("./routes/routes")
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
configure
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.configure(function(){
app.use(app.router)
app.engine("html", cons.hogan)
app.set("view engine", "html")
app.set("views", __dirname + "/views")
app.use(express.static(path.join(__dirname, "public")))
app.use(express.errorHandler())
})
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
routes
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.get("/", routes.index)
app.get("/hire", routes.hire)
app.get("/hire/:id/:nr", routes.hirePerson)
app.get("/books", routes.books)
app.get("/projects", routes.projects)
/*///////////////////////////////////////////////////////////////////////////////////////////////////////////
listen
///////////////////////////////////////////////////////////////////////////////////////////////////////////*/
app.listen(2020)
db.js
var mongoose = require('mongoose');
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM');
var Team = mongoose.model('Team');
Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
This .find method works in the shell, i get the json object returned, but when i'm using it in my node app the console spits out an empty object.
20 Feb 11:47:06 - [nodemon] restarting due to changes...
20 Feb 11:47:06 - [nodemon] starting `node app.js`
[]
thx for all your help guy's, really appreciate it. I'm getting there slowly but surely thx :)
EDIT 1
I changed db.js to this (using the comments below), but i still have an empty object.
var mongoose = require('mongoose');
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
var teamModel = mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM', function(err){doDBstuff(err)});
function doDBstuff(err){
if (err){throw err}
console.log("jow")
teamModel.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)});
}
EDIT 2
I created a new schema (booksSchema) and tried the code with the booksModel instead of the teamModel. The output was a correct JSON object. So it works with booksModel, but not with teamModel.
var mongoose = require('mongoose');
mongoose.set('debug', true)
var teamSchema = new mongoose.Schema({
country: String,
GroupName: String
});
var booksSchema = new mongoose.Schema({
title: String,
author: String
});
var teamModel = mongoose.model('Team', teamSchema);
var booksModel = mongoose.model('books', booksSchema);
mongoose.connect('mongodb://localhost/basingCOM', function(err){doDBstuff(err)});
function doDBstuff(err){
if (err){throw err}
booksModel.find(function (err, books) {console.log(books)});
}
some console output:
> db.Team.find()
{ "_id" : ObjectId("5305d71aa753d02674ed311c"), "country" : "belgium", "GroupName" : "kevin" }
{ "_id" : ObjectId("5305d738a753d02674ed311d"), "country" : "holland", "GroupName" : "dave" }
>
> db.Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
{ "_id" : ObjectId("5305d71aa753d02674ed311c"), "country" : "belgium", "GroupName" : "kevin" }
>
EDIT 3
when turning on mongoose.set("debug", true) it shows that the mongo is looking for the data in the collection teams.find() instead of Team.find().
20 Feb 13:46:29 - [nodemon] starting `node app.js`
Mongoose: teams.find({}) { fields: undefined }
[]
I think this is because you are executing the query before a connection is established with MongoDB. mongoose.connect is an asynchronous function and you have to wait for it's callback to be invoked before you do the query. Try like this...
mongoose.connect('mongodb://localhost/basingCOM', function (err) {
if (err)
throw err;
console.log("Successfully connected to MongoDB");
Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
});
Try to change the
mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM');
var Team = mongoose.model('Team');
Team.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)})
to
var teamModel = mongoose.model('Team', teamSchema);
mongoose.connect('mongodb://localhost/basingCOM');
teamModel.find({"GroupName":"kevin"}, function (err, teams) {console.log(teams)});
Normally I do the differente way, but I think that this way can help you....