MongoDB Upload Err - Error: key $clusterTime must not start with '$' - mongodb

I am getting an unusual error message when trying to insert data into my mongodb database. I am attempting to perform a simple insertOne operation, not even referencing any key called $clusterTime
db.collection("listings").insertOne( objToInsert , function(err, res) {
if (err) { console.log(err); console.log("err"); return; }
console.log('success');
});

Solution provided by Mongodb at : https://jira.mongodb.org/browse/NODE-1627?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel
Basically, upgrade from v3.1.2 to v3.1.3

Related

Error updating mongoDB document using Postman

I am trying to update a record from mongoDB collection using Postman.
This is the code I am using to do it.
// Update message with id (using a PUT at http://localhost:8080/messages/:message_id)
router.route('/messages/:message_id')
.put(function(req, res) {
Message.findById(req.params.message_id, function(err, message) {
if (err)
res.send(err);
// Update the message text
message.text = req.body.text;
message.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Message successfully updated!' });
});
});
});
//Updating A Message end.
Next, this is the URI I am typing in Postman to update (based on the id key):
localhost:8080/messages/ObjectId("58ab37f9d23f991791490963")
Then I get this error message:
I am trying to commit to a Bitbucket repository.
What should I change in the URI to make the update valid?
You may want to drop the ObjectId part from what you are posting and just send the id itself then create an ObjectId on the server
localhost:8080/messages/58ab37f9d23f991791490963
router.route('/messages/:message_id')
.put(function(req, res) {
var id = new ObjectId(req.params.message_id)
Message.findById(id, function(err, message) {
...
})

Express JS routing based on the user names

I am working on a express js project.I have got all my basic routing set up working perfectly. Usually when I want to search a record based on id I do this:
router.route('/sensors_home/:sensor_id')
.get(function (req, res) {
Sensor.findById(req.params.sensor_id,function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});
This allows me to retrieve the data when I do http://localhost:4000/sesnors_home/45000cbsfdhjbnabfbajhdb
(45000cbsfdhjbnabfbajhdb = Object id from the MongoDB )
Now my goal is to have several users to my application. I have my mongoose schema set up and the mongoDB looks like this :
Here is the issue: I wanna retrieve data corresponding to John Peterson based on his _id that is "John".Instead of doing this http://localhost:4000/sesnors_home/45000cbsfdhjbnabfbajhdb I wanna do something like this http://localhost:4000/sesnors_home/John and retrieve all the data specific to John. I tried various methods but still stuck with this issue. I tried using req.params._id and also some Mongodb queries on the User Collection but still no luck. Please suggest some ideas.
Thanks!
UPDATE:
I tried using the following code :
router.route('/sensors_home/:id')
.get(function (req, res) {
res.send(_id.toString());
User.findOne({_id: req.params._id} ,function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});
This gives me the following error :
ReferenceError: _id is not defined
Have you tried the following?
router.route('/sensors_home/:_id')
.get(function (req, res) {
Sensor.findOne({_id: req.params._id},function(err, sensorInfo) {
if (err)
res.send(err);
res.send(sensorInfo);
});
});

Unable to enter data in mongo database in express

router.get('/wiki/:topicname', function(req, res, next) {
var topicname = req.params.topicname;
console.log(topicname);
summary.wikitext(topicname, function(err, result) {
if (err) {
return res.send(err);
}
if (!result) {
return res.send('No article found');
}
$ = cheerio.load(result);
var db = req.db;
var collection = db.get('try1');
collection.insert({ "topicname" : topicname, "content": result }, function (err, doc){
if (err) {
// If it failed, return error
res.send("There was a problem adding the information to the database.");
}
else {
// And forward to success page
res.send("Added succesfully");
}
});
});
Using this code, I am trying to add the fetched content from Wikipedia in to the collection try1. The message "Added succesfully" is displayed. But the collection seems to be empty. The data is not inserted in the database
The data must be there, mongodb has { w: 1, j: true } write concern options by default so its only returns without an error if the document is truly inserted if there were any document to insert.
Things you should consider:
-Do NOT use insert function, its depricated use insertOne, insertMany or bulkWrite. ref.: http://mongodb.github.io/node-mongodb-native/2.1/api/Collection.html#insert
-The insert methods callback has two parameters. Error if there was an error, and result. The result object has several properties with could be used for after insert result testing like: result.insertedCount will return the number of inserted documents.
So according to these in your code you only test for error but you can insert zero documents without an error.
Also its not clear to me where do you get your database name from. Is the following correct in your code? Are you sure you are connected to the database you want to use?
var db = req.db;
Also you don't have to enclose your property names with " in your insert method. The insert should look something like this:
col.insertOne({topicname : topicname, content: result}, function(err, r) {
if (err){
console.log(err);
} else {
console.log(r.insertedCount);
}
});
Start your mongod server in a correct path,i.e, same path as that of what you are using to check the contents of collection.
sudo mongod --dbpath <actual-path>

concurrency issues while upserting and then reading the data from mongodb using mongoose

Hi I am trying to build an application which upserts data and fetches from the mongodb baser on the userid.This approach works fine for a single user.But when i try hitting for multiple users say 25 the data fetched seems to be null. Below is my upsert code
collection.update({'USER_ID': passVal.ID},
{'RESPONSE': Data}, { upsert: true }, function (err) {
if (err) {
console.log("Error in saving data");
}
var query = collection.findOne({'USER_ID': passVal.ID});
query.select('RESPONSE');
query.exec(function (err, data) {
if (err) return handleError(err);
console.log(data.RESPONSE);
});
})
I always get an error insome cases as data is null.I have written the read code in the call back of upsert only.I am stuck here any help regarding this will be much helpful.

Meteor error not being sent to client

I am trying to send a Meteor.Error to the client in the case where I have a duplicate key.
On the client, the 'error' in the callback is undefined (because at the moment, minimongo can't check for unique key indexes)
On the server the 'error' in the callback correctly throws an exception that a duplicate key exists and so it won't be inserted. However, the 'Meteor.Error' is never sent to the client.
Links.insert({
link: link_to_add,
user_id: this.userId
}, function(error, result) {
if (error != null) {
throw new Meteor.Error(409, 'Link already added');
}
});
What am I doing wrong? I am open to alternatives if there is a better way to do this.
It seems obvious to me now, but the problem was I wasn't capturing the error in the Meteor.call.
Meteor.call('add_link', {
link: link_to_add,
tag: tag_to_add
}, function(error, result) {
// the error is correctly being captured here
return console.log(error);
});