MongoDB Node check if objectid is valid - node.js

How can I check whether an ObjectID is valid using Node's driver
I tried :
var BSON = mongo.BSONPure;
console.log("Validity: " + BSON.ObjectID.isValid('ddsd'))
But I keep getting an exception instead of a true or false. (The exception is just a 'throw e; // process.nextTick error, or 'error' event on first tick'

This is a simple check - is not 100% foolproof
You can use this Regular Expression if you want to check for a string of 24 hex characters.
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
checkForHexRegExp.test("i am a bad boy")
// false
checkForHexRegExp.test("5e63c3a5e4232e4cd0274ac2")
// true
Regex taken from github.com/mongodb/js-bson/.../objectid.ts
For a better check use:
var ObjectID = require("mongodb").ObjectID
ObjectID.isValid("i am a bad boy")
// false
ObjectID.isValid("5e63c3a5e4232e4cd0274ac2")
// true
isValid code github.com/mongodb/js-bson/.../objectid.ts

isValid() is in the js-bson (objectid.ts) library, which is a dependency of node-mongodb-native.
For whoever finds this question, I don't recommend recreating this method as recommend in other answers. Instead, continue using node-mongodb-native like the original poster was using, the following example will access the isValid() method in js-bson.
var mongodb = require("mongodb");
var objectid = mongodb.BSONPure.ObjectID;
console.log(objectid.isValid('53fbf4615c3b9f41c381b6a3'));
July 2018 update: The current way to do this is:
var mongodb = require("mongodb")
console.log(mongodb.ObjectID.isValid(id))

As an extension of Eat at Joes answer... This is valid in node-mongodb-native 2.0
var objectID = require('mongodb').ObjectID
objectID.isValid('54edb381a13ec9142b9bb3537') - false
objectID.isValid('54edb381a13ec9142b9bb353') - true
objectID.isValid('54edb381a13ec9142b9bb35') - false

If you are using mongoose then you can use mongoose for validation rather than depending on any other library.
if (!mongoose.Types.ObjectId.isValid(req.id)) {
return res.status(400).send("Invalid object id");
}

#GianPaJ's snippet is great but it needs to be extended slightly to cover non hex objectID's. Line 32 of the same file indicates objectID's can also be 12 characters in length. These keys are converted to a 24 character hex ObjectID by the mongodb driver.
function isValidObjectID(str) {
// coerce to string so the function can be generically used to test both strings and native objectIds created by the driver
str = str + '';
var len = str.length, valid = false;
if (len == 12 || len == 24) {
valid = /^[0-9a-fA-F]+$/.test(str);
}
return valid;
}

Below is my model where I am trying to validate subject id that is of type objectId data using JOI (Joi.objectId().required()):
const Joi = require('joi');
const mongoose = require('mongoose');
const Category = mongoose.model('Category', new mongoose.Schema({
name: String
}));
function validateCategory(category) {
const schema = {
name: Joi.string().min(5).max(50).required(),
subject_id: Joi.objectId().required(),
};
return Joi.validate(category, schema);
}
exports.Category = Category;
exports.validate = validateCategory;
joi-objectid validates that the value is an alphanumeric string of 24 characters in length.
MongoDB ObjectId validator for Joi.

Follow this regular expression :
in js
new RegExp("^[0-9a-fA-F]{23}$").test("5e79d319ab5bfb2a9ea4239")
in java
Pattern.compile("^[0-9a-fA-F]{23}$").matcher(sanitizeText(value)).matches()

You can use Cerberus and create a custom function to validate and ObjectId
from cerberus import Validator
import re
class CustomValidator(Validator):
def _validate_type_objectid(self, field, value):
"""
Validation for `objectid` schema attribute.
:param field: field name.
:param value: field value.
"""
if not re.match('[a-f0-9]{24}', str(value)):
self._error(field, ERROR_BAD_TYPE % 'ObjectId')
## Initiate the class and validate the information
v = CustomValidator()
schema = {
'value': {'type': 'objectid'}
}
document = {
'value': ObjectId('5565d8adba02d54a4a78be95')
}
if not v(document, schema):
print 'Error'

Related

Joi "or" operator thinks an empty string is a valid input

I am trying to use the "or" operator in Joi ver.17.4.0
As you can see, in the code below, I want either or both of the attributes/properties to be allowed, but not neither.
The problem is that Joi does not allow a string to be empty. So, to have it empty, I need to:
Joi.string().allow('')
This makes it not empty according to the "or" operator. So I can not get the 'name' to be empty in the eyes of "or".
It won't validate properly.
It validates even when I do this (but it shouldn't):
validatePerson(createPerson(''));
Keep in mind that I'm actually validating POST input on a node express API, so this is some simplified code to illustrate the issue:
const Joi = require('Joi');
function createPerson(name, age) {
const person = { name: name, age: age };
console.log(person);
return person;
}
function validatePerson(person) {
const schema = Joi.object({
name: Joi.string().allow(''),
age: Joi.number(),
}).or("name", "age");
console.log(schema.validate(person));
return schema.validate(person);
}
validatePerson(createPerson('')); // This should fail validation but doesn't
validatePerson(createPerson()); // Should fail and does
validatePerson(createPerson('Bob')); // Should pass and does
validatePerson(createPerson('', 7)); // Should pass and does
validatePerson(createPerson('Bob', 7)); // Should pass and does
As far as I understand, you want to allow the name to be empty an string, only if the age exists.
To acheive that, you can use .when:
name: Joi.string().when('age', { is: Joi.exist(), then: Joi.allow('') })
This way, your first example will fail as you expected.

mongoose, getting object's id after using collection.insert

I'm trying to get the object id after adding it to the db (using collection.insert)
mongoose.model('Persons').collection.insert(person, function(err, newPerson) {
console.log('lets see you', newPerson);
});
and from the console I'm getting only result: { ok: 1, n: 1 } in stand of the new obj, any ideas how can I rich to the new object ?
thanks!
You can use save() here
var Persons = mongoose.model('Persons');
var personJSON = {
..... // persons schema values you want to insert
};
var person = new Persons(personJSON);
var result = yield person.save();
result variable will contain all the fields you inserted along with _id

How to find a mongoId in an array of mongoIds outside a db call

I'm aware its possible to find this out via a db call, but out of curiosity for instance in node If I had an array of mongoose document ids. How could I emulate an indexOf function against that array to determine if another mongoId is in it?
I only ask since doing a direct comparison of ids doesn't work and requires the use of `objectId.equals()' function. Is there a mongoose/mongoId function that does something similar to indexOf for ids?
If your problem is that you are not sure which is a string or which is an ObjectId in your comparisons then just convert everything to a "string". All objects have .toString(), even strings. And for array transformation just use .map().
Example:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var objSchema = new Schema({
});
var Obj = mongoose.model('Obj',objSchema);
var arrayTest1 = [],
arrayTest2 = [];
for ( x = 0; x < 10; x++ ) {
var obj = new Obj();
arrayTest1.push( obj.id );
arrayTest2.push( obj.id.toString() );
}
var el1 = arrayTest1[4],
el2 = arrayTest2[3];
[el1,el2].forEach(function(el) {
console.log( el );
});
console.log( arrayTest1.indexOf(el1) );
console.log( arrayTest2.indexOf(el2) );
console.log( arrayTest1.map(function(el) {
return el.toString()
}).indexOf(el1.toString()));
console.log( arrayTest2.map(function(el) {
return el.toString()
}).indexOf(el2.toString()));
Both pairs of statement evaluate to their correct index matches. So the second form there is universal to the object type. ObjectId or sting on either side.

Why are mongoose references turning into IDs?

According to http://mongoosejs.com/docs/populate.html, if I set a ref property to an object, and not an ID, when getting it, I should get back an object and not an ID. I'm referring to this part of the page:
var guille = new Person({ name: 'Guillermo' });
guille.save(function (err) {
if (err) return handleError(err);
story._creator = guille;
console.log(story._creator.name);
// prints "Guillermo" in mongoose >= 3.6
// see https://github.com/LearnBoost/mongoose/wiki/3.6-release-notes
Here's my sample code:
var T1schema = new mongoose.Schema({
otherModel:{type:mongoose.Schema.Types.ObjectId, ref:"T2"}
});
var T1 = mongoose.model('T1', T1schema);
var T2schema = new mongoose.Schema({
email: {type: String},
});
var T2 = mongoose.model('T2', T2schema);
var t1 = new T1();
var t2 = new T2({email:"foo#bar.com"});
t1.otherModel = t2;
Now when I refer to t1.otherModel, it's just an ObjectId, and not a T2. For example:
console.log(t1.otherModel.email);
prints undefined instead of "foo#bar.com". Any ideas why? Note I'm using Mongoose 3.6.18 according to it's package.json.
Thanks!
I think your expectations here just don't match what mongoose does. The schema is a way of saying "model M's property P will always be of type T". So when you set a value, mongoose uses the schema definitions to cast the set value to the type the schema requires. Here's a little REPL session. Note setting a number property with a string value casts it to a number, but trying to store a boolean in a number field just ignores the errant value.
> var mongoose = require('mongoose')
> var schema = new mongoose.Schema({prop1: Number})
> var Model = mongoose.model('Model', schema)
> var m = new Model
> m.prop1 = 42
42
> m.prop1
42
> m.prop1 = "42"
'42'
> m.prop1
42
> m.prop1 = false
false
> m.prop1
42
So when your schema says something is going to be an ObjectId, if you give it a model instance, mongoose immediately converts it to an ObjectId in preparation for a write to the database, which is the common case. Normally if you just set that model instance, you don't need to get it back out of the parent model before saving the parent model to the database.
So the model instance getter defined properties are always going to return something compatible with the schema, and populate has to do with loading refs from the DB, but for whatever reason mongoose just doesn't work the same comparing a .populated instance with a non-populated instance. I can see why this is confusing and perhaps unexpected/disappointing though.
Mongoose is normalizing the instance to match the Schema, which specifies that otherModel is an ObjectId.
otherModel:{type:mongoose.Schema.Types.ObjectId, ref:"T2"}
So, Mongoose treats t1.otherModel = t2; the same as:
t1.otherModel = t2._id;
The ref isn't used until .populate() is called (either directly on the document or in a query), which needs both objects to be saved:
t2.save(function (err) {
t1.save(function (err) {
console.log(t1.otherModel);
// 7890ABCD...
t1.populate('otherModel', function () {
console.log(t1.otherModel);
// { email: 'foo#bar.com', _id: 7890ABCD..., __v: 0 }
});
});
});

nodejs mongodb object id to string

IN nodejs, with mongodb, mongoosejs as orm
I am doing this
I have a model, User
User.findOne({username:'someusername'}).exec(function(err,user){
console.log(user) //this gives full object with something like {_id:234234dfdfg,username:'someusername'}
//but
console.log(user._id) //give undefined.
})
Why? And how to get the _id to string then?
Try this:
user._id.toString()
A MongoDB ObjectId is a 12-byte UUID can be used as a HEX string representation with 24 chars in length. You need to convert it to string to show it in console using console.log.
So, you have to do this:
console.log(user._id.toString());
Take the underscore out and try again:
console.log(user.id)
Also, the value returned from id is already a string, as you can see here.
I'm using mongojs, and I have this example:
db.users.findOne({'_id': db.ObjectId(user_id) }, function(err, user) {
if(err == null && user != null){
user._id.toHexString(); // I convert the objectId Using toHexString function.
}
})
try this:
objectId.str;
see doc.
If you're using Mongoose, the only way to be sure to have the id as an hex String seems to be:
object._id ? object._id.toHexString():object.toHexString();
This is because object._id exists only if the object is populated, if not the object is an ObjectId
When using mongoose .
A representation of the _id is usually in the form (received client side)
{ _id: { _bsontype: 'ObjectID', id: <Buffer 5a f1 8f 4b c7 17 0e 76 9a c0 97 aa> },
As you can see there's a buffer in there. The easiest way to convert it is just doing <obj>.toString() or String(<obj>._id)
So for example
var mongoose = require('mongoose')
mongoose.connect("http://localhost/test")
var personSchema = new mongoose.Schema({ name: String })
var Person = mongoose.model("Person", personSchema)
var guy = new Person({ name: "someguy" })
Person.find().then((people) =>{
people.forEach(person => {
console.log(typeof person._id) //outputs object
typeof person._id == 'string'
? null
: sale._id = String(sale._id) // all _id s will be converted to strings
})
}).catch(err=>{ console.log("errored") })
function encodeToken(token){
//token must be a string .
token = typeof token == 'string' ? token : String(token)
}
User.findOne({name: 'elrrrrrrr'}, function(err, it) {
encodeToken(it._id)
})
In mongoose , the objectId is an object (console.log(typeof it._id)).
starting from Mongoose 5.4, you can convert ObjectId to String using SchemaType Getters.
see What's New in Mongoose 5.4: Global SchemaType Configuration.
Access the property within the object id like that user._id.$oid.
Really simple use String(user._id.$oid)
The result returned by find is an array.
Try this instead:
console.log(user[0]["_id"]);
try this : console.log(user._doc._id)

Resources