-3

I'm new to MongoDB and trying to figure out some solutions for basic requirements.

Here is the scenario: I have an application which saves info to MongoDB through a scheduler. Each time when the scheduler runs, i need to fetch the last inserted document from db to get the last info id which i should send with my request to get the next set of info. Also when i save an object, i need to validate it whether a specific value for a field is already there in db, if it is there, i just need to update a count in that document or else will save a new one.

My Questions are:

  • I'm using MongoDB java driver. Is it good to generate the object id myself, or is it good to use what is generated from the driver or MongoDB itself?

  • What is the best way to retrieve the last inserted document to find the last processed info id?

  • If i do a find for the specific value in a column before each insert, i'm worried about the performance of the application since it is supposed to have thousands of records. What is the best way to do this validation?

  • I saw in some places where they are talking about two writes when doing inserts, one to write the document to the collection, other one to write it to another collection as last updated to keep track of the last inserted entry. Is this a good way? We don't normally to this with relational databases.

  • Can i expect the generated object ids to be unique for every record inserted in the future too? (Have a doubt whether it can repeat or not)

    Thanks.

Community
  • 1
  • 1
popcoder
  • 2,952
  • 6
  • 32
  • 47
  • 1
    Too many questions here. With a little searching, you'll find answers to each one of these. `ObjectIds` are always unique and include timestamp. http://stackoverflow.com/questions/3305561/how-to-query-mongodb-with-like – WiredPrairie Jul 07 '13 at 10:53
  • These are multiple unrelated questions, many of them quite common. Please check the search function for answers to them and then create separate questions for each one you can't find an answer for. – Philipp Jul 07 '13 at 10:56
  • @WiredPrairie The funny part that this is the Google result. – thedp Dec 21 '13 at 16:35
  • Not surprising at all with stackoverflow questions. It happens all the time as Google aggressively indexes the site. – WiredPrairie Dec 21 '13 at 18:22

1 Answers1

1

Can i expect the generated object ids to be unique for every record inserted in the future too? (Have a doubt whether it can repeat or not)

I'm using MongoDB java driver. Is it good to generate the object id myself, or is it good to use what is generated from the driver or MongoDB itself?

If you don't provide a object id for the document, mongo will automatically generate it for you. All documents must definitely have _id. Here is the relevant reference.

The relevant part is
    ObjectId is a 12-byte BSON type, constructed using:
    
    a 4-byte value representing the seconds since the Unix epoch,
    a 3-byte machine identifier,
    a 2-byte process id, and
    a 3-byte counter, starting with a random value.

http://docs.mongodb.org/manual/reference/object-id/

I guess this is more than enough randomization(although a poor hash choice, since dates/time increase monotonically)

If i do a find for the specific value in a column before each insert, i'm worried about the performance of the application since it is supposed to have thousands of records. What is the best way to do this validation?

Index the field(there are no columns in mongodb, 2 documents can have different fields) first.

   db.collection_name.ensureIndex({fieldName : 1})

What is the best way to retrieve the last inserted document to find the last processed >info id?

Fun fact. We don't need info id field if we are using it once and then deleting the document. The _id field is datewise sorted. But if the document is regularly updated, then we need to modify the document atomically with the find and modify operation.

http://api.mongodb.org/java/2.6/com/mongodb/DBCollection.html#findAndModify%28com.mongodb.DBObject,%20com.mongodb.DBObject,%20com.mongodb.DBObject,%20boolean,%20com.mongodb.DBObject,%20boolean,%20boolean%29

Now you have updated the date of the last inserted/modified document. Make sure this field is indexed. Now using the above link, look for the sort parameter and populate it with

    new BasicDbObject().put("infoId", -1); //-1 is for descending order.

I saw in some places where they are talking about two writes when doing inserts, one to write the document to the collection, other one to write it to another collection as last updated to keep track of the last inserted entry. Is this a good way? We don't normally to this with relational databases.

Terrible Idea ! Welcome to Mongo. You can do better than this.

Community
  • 1
  • 1
bsd
  • 2,707
  • 1
  • 17
  • 24