2

I'm using MongoDB for my senior project to allow donors to sponsor children for an organization similar to world vision and I'm wondering about the feasibility of reusing the ObjectID datatype as a unique id within a json array.

example donor document:

{
    _id: ObjectID(x),  // this document's _id
    name: "Josh",
    last_name: "Richard",
    address: "2 Happy Lane",
    city: "New York",
    state: "New York",
    credit_card: 999999999999999,
    cvv: 999,
    exp: 3/18,
    transactions: [
        {
            _id: ObjectID(x),  // this is what i'm asking about
            child_ids: [
                ObjectID(x),   // this is the _id of a doc in another collection
                ObjectID(x)    // this is also the _id of a doc in another collection
            ]
        },
        {
            _id: ObjectID(x),
            child_ids: [
                ObjectID(x),   // this is the _id of a doc in another collection
            ]
        }
    ]
}

The transactions._id is what I'm looking for feedback on. I need this to be unique so that I can differentiate orders from each other, but I'd rather not create a new collection for storing these as documents and instead keep it all in the donor doc. Any thoughts?

EDIT: What I'm not curious about is the technical uniqueness of an ObjectID. What I'm looking to find out is if it's realistic to "repurpose" the ObjectID as a unique key to then use later in application logic. I need some way to uniquely identify objects in an array across all instances of that type of array in every document. I.E.: Every transaction needs to have it's own unique identifier and those transactions are stored in arrays in many different documents within the same collection.

Joshua
  • 21
  • 4
  • have a look at `$addToSet` [link](https://docs.mongodb.org/manual/reference/operator/update/addToSet/) – Syed Faizan Mar 23 '16 at 06:17
  • Possible duplicate of [Possibility of duplicate Mongo ObjectId's being generated in two different collections?](http://stackoverflow.com/questions/4677237/possibility-of-duplicate-mongo-objectids-being-generated-in-two-different-colle) – Blakes Seven Mar 23 '16 at 06:34
  • So every time you ask for a generated `ObjectId` it is guaranteed ( well most reasonably always ) unique. – Blakes Seven Mar 23 '16 at 06:36

0 Answers0