How to use the mongodb.ObjectID 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 Asymmetrik / node-fhir-server-mongo / src / services / bodysite / bodysite.service.js View on Github external
module.exports.create = (args, context, logger) => new Promise((resolve, reject) => {
	logger.info('BodySite >>> create');

	let { base_version, resource } = args;
	// Make sure to use this ID when inserting this resource
	let id = new ObjectID().toString();

	let BodySite = getBodySite(base_version);
	let Meta = getMeta(base_version);

	// TODO: determine if client/server sets ID

	// Cast resource to BodySite Class
	let bodysite_resource = new BodySite(resource);
	bodysite_resource.meta = new Meta();
	// TODO: set meta info

	// TODO: save record to database

	// Return Id
	resolve({ id });
});
github IdeaBlade / Breeze / _Internal / Mongo / breeze-mongodb / mongoSaveHandler.js View on Github external
that._coerceData(e, entityType, resourceName);
        var entityKey = { entityTypeName: entityTypeName, _id: e._id } ;
        entityAspect.entityKey = entityKey;
        if (entityState === "Added") {
            delete e.entityAspect;

            var autoGeneratedKeyType = entityType.autoGeneratedKeyType;
            if (autoGeneratedKeyType && autoGeneratedKeyType !== "None") {

                var keyDataType = entityType.keyDataType;
                if (keyDataType === "Guid") {
                    e._id = createGuid();
                } else if (keyDataType == "MongoObjectId") {
                    // instead of omitting the _id and having mongo update it, we want to set it ourselves so that we can do
                    // fk fixup before going async
                    e._id = new ObjectID();
                } else {
                    that._raiseError(new Error("ObjectIds and Guids are the only autoGenerated key types that Breeze currently supports, not: " + keyDataType));
                    return;
                }
            }

            // entityKey._id may be null/undefined for entities created only on the server side - so no need for keyMapping
            if (entityKey._id == null) {
                entityKey._id = e._id;
            } else if (entityKey._id !== e._id) {
                keyMapping = { entityTypeName: entityTypeName, tempValue: entityKey._id, realValue: e._id };
                that._keyMappings.push(keyMapping);
            }

            var insertDoc = {
                entity: e,
github Asymmetrik / node-fhir-server-mongo / src / services / procedure / procedure.service.js View on Github external
module.exports.create = (args, context, logger) => new Promise((resolve, reject) => {
	logger.info('Procedure >>> create');

	let { base_version, resource } = args;
	// Make sure to use this ID when inserting this resource
	let id = new ObjectID().toString();

	let Procedure = getProcedure(base_version);
	let Meta = getMeta(base_version);

	// TODO: determine if client/server sets ID

	// Cast resource to Procedure Class
	let procedure_resource = new Procedure(resource);
	procedure_resource.meta = new Meta();
	// TODO: set meta info

	// TODO: save record to database

	// Return Id
	resolve({ id });
});
github Tarnadas / smmdb / website / src / server / Database.ts View on Github external
public static async publishBlogPost ({ accountId, blogId, markdown }: any): Promise {
    console.log({ accountId, blogId, markdown })
    if (!blogId) {
      blogId = (await this.blog.insertOne({ accountId: new ObjectID(accountId), markdown, isCurrent: true })).insertedId
    }
    let blogPost = await this.blog.find({
      accountId: new ObjectID(accountId), _id: new ObjectID(blogId)
    }).toArray()
    if (!blogPost || blogPost.length === 0) {
      return {
        err: `Blog post with id ${blogId} was not found or marked as publishable`
      }
    }
    blogPost = blogPost[0]
    const published = Math.trunc(Date.now() / 1000)
    await this.blog.updateOne({ _id: blogPost._id }, {
      $set: Object.assign({
        isCurrent: false,
        markdown
      }, blogPost.isCurrent ? { published } : { edited: published })
github jaystack / odata-v4-server / lib / example / categories.js View on Github external
"use strict";
const mongodb_1 = require("mongodb");
module.exports = [
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af289"), "Description": "Soft drinks", "Name": "Beverages" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28a"), "Description": "Breads", "Name": "Grains/Cereals" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28b"), "Description": "Prepared meats", "Name": "Meat/Poultry" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28c"), "Description": "Dried fruit and bean curd", "Name": "Produce" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28d"), "Description": "Seaweed and fish", "Name": "Seafood" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28e"), "Description": "Sweet and savory sauces", "Name": "Condiments" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af28f"), "Description": "Cheeses", "Name": "Dairy Products" },
    { "_id": new mongodb_1.ObjectID("578f2baa12eaebabec4af290"), "Description": "Desserts", "Name": "Confections" }
];
//# sourceMappingURL=categories.js.map
github juicycleff / ultimate-backend / libs / nest-multi-tenant / src / database / mongo / repository / base-mongo.repository.ts View on Github external
if (replace) {
            doc._id = new ObjectID(doc.id);
            delete doc.id;
          } else {
            doc.id = doc._id.toString();
            delete doc._id;
          }
        }
        docs.push(doc);
      }
      return docs;
    }

    if (document && (document.id || document._id)) {
      if (replace) {
        document._id = new ObjectID(document.id);
        delete document.id;
      } else {
        document.id = document._id.toString();
        delete document._id;
      }
    }
    return document;
  }
github ptisp / cloudash / lib / routes / user.js View on Github external
}, function(err, doc) {
    if (err || !doc) {
      return res.status(500).json({
        'error': 'Error fetching user (DB)'
      });
    }

    doc.token = crypto.randomBytes(10).toString('hex');

    user.update({
      _id: ObjectId(doc._id)
    }, doc, function(err, output) {
      if (err) {
        return res.status(500).json({
          'error': 'Error saving USER details (DB)'
        });
      }

      vendors.email(config.url + '/api/user/' + doc.auth.username + '/recover/' + doc.token, doc.auth.username, 'Password recovery');

      return res.status(200).json({});
    });
  });
};
github dmcaulay / mongo-wrapper / index.js View on Github external
var addIdQuery = function(args) {
  args = Array.prototype.slice.call(args)
  var id = args.shift()
  if (_.isString(id)) id = new mongo.ObjectID(id)
  args.unshift({_id: id})
  return args
}
github mozilla / openbadges-discovery / app / api.js View on Github external
app.delete('/pathway/:pid/requirement/:rid', function (req, res, next) {
    var rid = new ObjectID(req.params.rid);

    db.requirements.remove({_id: rid}, function (err, num) {
      if (err) throw err;
      return res.send(200);
    });
  });
github microservices-demo / carts / api-spec / hooks.js View on Github external
const address = [
    {"_id":ObjectID("579f21ae98684924944651bd"),"_class":"works.weave.socks.users.entities.Address","number":"69","street":"Wilson Street","city":"Hartlepool","postcode":"TS26 8JU","country":"United Kingdom"},
    {"_id":ObjectID("579f21ae98684924944651c0"),"_class":"works.weave.socks.users.entities.Address","number":"122","street":"Radstone WayNet","city":"Northampton","postcode":"NN2 8NT","country":"United Kingdom"},
    {"_id":ObjectID("579f21ae98684924944651c3"),"_class":"works.weave.socks.users.entities.Address","number":"3","street":"Radstone Way","city":"Northampton","postcode":"NN2 8NT","country":"United Kingdom"}
];


const card = [
    {"_id":ObjectID("579f21ae98684924944651be"),"_class":"works.weave.socks.users.entities.Card","longNum":"8575776807334952","expires":"08/19","ccv":"014"},
    {"_id":ObjectID("579f21ae98684924944651c1"),"_class":"works.weave.socks.users.entities.Card","longNum":"8918468841895184","expires":"08/19","ccv":"597"},
    {"_id":ObjectID("579f21ae98684924944651c4"),"_class":"works.weave.socks.users.entities.Card","longNum":"6426429851404909","expires":"08/19","ccv":"381"}
];

const cart = [
    {"_id":ObjectID("579f21de98689ebf2bf1cd2f"),"_class":"works.weave.socks.cart.entities.Cart","customerId":"579f21ae98684924944651bf","items":[{"$ref":"item","$id":ObjectID("579f227698689ebf2bf1cd31")},{"$ref":"item","$id":ObjectID("579f22ac98689ebf2bf1cd32")}]},
    {"_id":ObjectID("579f21e298689ebf2bf1cd30"),"_class":"works.weave.socks.cart.entities.Cart","customerId":"579f21ae98684924944651bfaa","items":[]}
];


const item = [
    {"_id":ObjectID("579f227698689ebf2bf1cd31"),"_class":"works.weave.socks.cart.entities.Item","itemId":"819e1fbf-8b7e-4f6d-811f-693534916a8b","quantity":20,"unitPrice":99.0}
];


const customer = [
    {"_id":"579f21ae98684924944651bf","_class":"works.weave.socks.users.entities.Customer","firstName":"Eve","lastName":"Berger","username":"Eve_Berger","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651bd")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651be")}]
    },
    {"_id":"579f21ae98684924944651c2","_class":"works.weave.socks.users.entities.Customer","firstName":"User","lastName":"Name","username":"user","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651c0")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651c1")}]},
    {"_id":"579f21ae98684924944651c5","_class":"works.weave.socks.users.entities.Customer","firstName":"User1","lastName":"Name1","username":"user1","addresses":[{"$ref":"address","$id":ObjectID("579f21ae98684924944651c3")}],"cards":[{"$ref":"card","$id":ObjectID("579f21ae98684924944651c4")}]}
];