Loopback Find is returning Empty - node.js

Database : Mongo 3.4+
NodeJS:v6.9.4
OS: Centos 7+
In mongo shell, following command return result,
db.processticket.find({"parentProcessID": "5978ab9f82c56ec868d0d002"})
however, following code find/findOne returns empty result
app.models.processticket.findOne({
where: {"parentProcessID": "5978ab9f82c56ec868d0d002" }
}, function(err, result) {

You can use strictObjectIDCoercion flag in model definition file. Reference

Please wrap the value of parentProcessID in ObjectId function. Since mongo saves Id values as ObjectId in your where query you need to warp the value of your parentProcessID with ObjectId function. You can find ObjectID function in native mongodb module
const ObjectID = require('mongodb').ObjectID;

Related

Cast to ObjectId failed for value "some_id" at path "_id" for model "Category"

when I use findOne from mongoose version 5.9.12, I got some error like this :
error
https://i.stack.imgur.com/8kAcY.png
my code:
[code][2]
https://i.stack.imgur.com/v8Prd.png
my models:
models
From the mongoose documentation:
If you want to query by a document's _id, use findById() instead of
findOne().
The id is cast based on the Schema before sending the command.
So the recommended way to do this is:
Category.findById(categoryId)
If you really want to use findOne and specify _id inside the filter, you can convert the string using
Category.findOne( { _id: mongoose.Types.ObjectId(categoryId) })
to an ObjectId.
Edit: The actual problem is that your categoryId contains a trailing whitespace which causes this problem: "5f05445a9789663d08fb12d2 " - it should be "5f05445a9789663d08fb12d2".

Converting string to ObjectId is failing in mongoose 4.6.0

I am trying to convert a string to ObjectId using
var body={};
var objId="57b40595866fdab90268321e";
body.id=mongoose.Types.ObjectId(objId);
myModel.collection.insert(body,function(err,data){
//causing err;
});
the above code is working fine when mongoose 4.4.16 is used, but if i update my mongoose to latest version(4.6.0) then problem occurs.
Err
object [
{
"_bsontype":"ObjectID",
"id:{"0":87,"1":180,"2":5,"3":235,"4":134,"5":111,"6":218,"7":185,"8":2,"9":104,"10":50,"11":111}
}
]
is not a valid ObjectId
The right way to insert new document is-
var newDocument = new myModel({
_id: mongoose.Types.ObjectId("57b40595866fdab90268321e")
});
newDocument.save();
In you case-
It stops working because the differences between versions of mongoose and mongo native drivers.
although, you are able to perform this by the example above, or, if you still want to use insert, you can use the myModel.insertMany (by passing object instead of array)
look here
http://mongoosejs.com/docs/api.html#model_Model.insertMany
I don't have the time to spike it, but if I remember correctly id is a simple string and _id is the ObjectId, i.e. either
body.id="57b40595866fdab90268321e"
or
body._id=mongoose.Types.ObjectId("57b40595866fdab90268321e");
That said, does it have to be that specific id? If not, you can use new myModel() and an id will be automatically created.

findOne not returning correct result( Mongo DB and nodejs)

I have stored a document in collection and when i am trying to retrieve it via findOne, it is returning me wrong result:
My Mongoose model is like:
var db = require('../db');
var mongoose = db.mongoose_var;
var companySchema = mongoose.Schema({
companyName:String
});
module.exports = mongoose.model('Company',companySchema);
Which then i am using in my server.js as :
var CompanySchema = require('./schemas/companySchema');
and when I am trying to find already inserted record as following:
CompanySchema.findOne({'Company.companyName':jsonObj.companyName},function(err,companyName){
console.log('companyName foudn:'+companyName);
if(companyName !== null){
res.status(404).json({status:'Name already in the DB'});
return;
}else{...
Its unable to find this record, but its returning probably the first record.
Folloing record is present in thedb:
When I am trying to add another company and using this findOne to check if this name already exists, findOne returns this record only. My log snippets
whereas mongo shell returns proper result only.
In mongo shell, I am using " " around field and values, not in findOne Api.
Thanks in Advance
You should probably use the following :
CompanySchema.findOne({'companyName':jsonObj.companyName},function(err,companyDocument){
if(err) console.log(err);
console.log('company found:' + companyDocument);
if(companyDocument){
res.status(404).json({status:'Name already in the DB'});
return;
}else{...
The mistake in your code was that you were using Company.companyName instead of companyName. companyName is the name of the field in the Company collection.
Hope this helps you.

MongoDB retrieval error

I am retrieving a document from MongoDB using find() in Node.js, while printing the result I am not getting the retrieved values. Here my code..
db.collection("Product").find({'entry_id':entryID},function(err, result) {
console.log("Output:",result);
You could just about place money on entry_id is an ObjectId value and what you are passing as a variable is actually just a string.
But the other obvious thing is that you are using .find() in the wrong way. The "result" returned is a
"cursor". If you want something that looks like a whole result set, then use .toArray() or other similar method of conversion:
var ObjectID = require('mongodb').ObjectID;
db.collection("Product").find({
'entry_id': new ObjectID(entryID)
}).toArray(function(err, result) {
console.log("Output:",result);
});

Not able to remove document from mongo in Node

I'm trying to remove a document from a mongo database using Node. Is something wrong with my .remove method here:
deleteApplicant : function(req,res){
applicationModel.remove({_id:req.params.id} , function(err,count){
res.redirect('/applicants');
console.log(count)
})
}
Am I using the correct syntax?
_id in the database is probably of type ObjectId, but you're passing a string to the query.
Try this instead:
var ObjectId = require('mongodb').ObjectID;
...
applicationModel.remove({ _id : ObjectId(req.params.id) }, ...)

Resources