How to use the mongodb.ObjectID.createFromHexString function in mongodb

To help you get started, we’ve selected a few mongodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pillarsjs / pillars / lib / modelator / mongoInterface.js View on Github external
var query = {}, cols = {};
	var keys = (user && Array.isArray(user.keys))?user.keys:[];

	var grant = false;
	if(schema.keysCheck(keys,'get')){
		grant = true;
	} else if(user && schema.keysCheck(keys.concat(['owner']),'get')){
		grant = true;
		query.$or = [{_author:user._id},{_guests:user._id}];
	}

	if(grant){
		var path = params.path || ''; // Warning, clean first and end '/'.
		var _id = path.split('/').slice(2,3).join();
		if(/^[a-f0-9]{24}$/i.test(_id)){
			_id = new ObjectID.createFromHexString(_id);
		} else {
			_id = false;
		}

		if(_id){
			var field = path.split('/').pop();
			query._id = _id;
			cols[field]=1;
			var db = DB.collection(schema.collection);
			db.findOne(query,cols,function(error, result) {
				if(error){
					cb({
						error : 500,
						details : error
					});
				} else if(!result) {
github pillarsjs / pillars / pillars.js View on Github external
function saveSession(){
		var sid = gw.cookie.sid || false;
		var sessions = server.database.collection('sessions');
		if(sid){
			sid = new ObjectID.createFromHexString(sid);
			sessions.update({_id:sid},gw.session,function(error, result) {
				if(!error && result>0){
					// Ok
				} else {
					console.log("Error on session save.");
				}
			});
		}
	}
github SierraSoftworks / Iridium / test / diff.js View on Github external
it('should correctly diff ObjectIDs', function() {
        var o1 = new ObjectID();
        var o2 = ObjectID.createFromHexString(o1.toHexString());

        var a1 = { _id: o1 };
        var a2 = { _id: o2 };
        var expected = {
            
        };

        diff(a1, a2).should.eql(expected);

        o2 = new ObjectID();

        var a1 = { _id: o1 };
        var a2 = { _id: o2 };
        var expected = {
            $set: { _id: o2 }
        };
github pillarsjs / pillars / pillars.js View on Github external
function getSession(callback){
		var sid = gw.cookie.sid || false;
		var sessions = server.database.collection('sessions');
		if(!sid){
			newSession(callback);
		} else {
			sid = new ObjectID.createFromHexString(sid);
			sessions.findOne({_id:sid},function(error, result) {
				if(!error && result){
					gw.session = result;
				} else {
					console.log("Error on get session.");
				}
				callback();
			});
		}
	}
github pillarsjs / pillars / lib / plugins / accounts.js View on Github external
module.exports = new Plugin({id:'Accounts'},function(gw,next){
	var account = gw.routing.check('account',false);
	if(account){
		if(gw.user){
			next();
		} else if(gw.session.user){
			var _id = gw.session.user;
			var users = DB.collection('users');
			if(/^[a-f0-9]{24}$/i.test(_id)){_id = new ObjectID.createFromHexString(_id);}
			users.findOne({_id:_id},function(error, result) {
				if(!error && result){
					var user = new User(result);
					gw.user = user;
					gw.emit('user',user);
					next();
				} else {
					delete gw.session.user;
					gw.error(403);
				}
			});
		} else {
			gw.error(403);
		}
	} else {
		next();
github lmachens / copypasta / lib / pastes.js View on Github external
async function getPaste(pasteId) {
  const pastesCollection = await getCollection(collectionName);
  const objectId = new ObjectID.createFromHexString(pasteId);
  const paste = await pastesCollection.findOne({ _id: objectId });
  if (!paste) {
    throw new Error("Can not find paste");
  }
  return paste;
}
github niklaskorz / nkchat / server / src / scalars / ObjectIDScalar.ts View on Github external
parseValue(value: string) {
    return ObjectID.createFromHexString(value);
  },
  parseLiteral(node: ValueNode) {
github pdxphilippmac / patchfox / server / lib / posts.js View on Github external
async function getPaste(pasteId) {
  const pastesCollection = await getCollection(collectionName);
  const objectId = new ObjectID.createFromHexString(pasteId);
  const paste = await pastesCollection.findOne({ _id: objectId });
  if (!paste) {
    throw new Error("Can not find paste");
  }
  return paste;
}
github niklaskorz / nkchat / server / src / scalars / ObjectIDScalar.ts View on Github external
parseLiteral(node: ValueNode) {
    if (node.kind !== Kind.STRING) {
      throw new Error('Cannot parse non-string as ObjectID');
    }
    return ObjectID.createFromHexString(node.value);
  },
  serialize(value: ObjectID) {
github howarddierking / RestBugs / RestBugs-Node / lib / bugsdata.js View on Github external
ensureDbClient(function(err, db){
		if(err) return callback(err, null);
		db.collection(bugsCollection).findOne({ _id: new ObjectID.createFromHexString(bugID) }, function(err, data){
			if(err) return callback(err, null);
			callback(null, bugs.from(data));
		});
	});
};