I'm working on a MongoDB project which stores tweets and was created by someone else.
This person decided to use the Twitter tweet ID for the _id
field in MongoDB, which means I now have no way to sort the tweets deterministically.
Example:
> db.tweets.find().sort({_id : 1}).limit(4)
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(1)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(2)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(3)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(5)}
The reason sorting on the field ID is non-deterministic is that at a later date, my system could add the existing tweet that has an ID of 4 to the database, meaning that the same command would give a different result set:
> db.tweets.find().sort({_id : 1}).limit(4)
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(1)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(2)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(3)}
{'message' : '...', 'userId' : NumberLong(123), '_id' : NumberLong(4)}
My question is: is there a way to add a new 'field' to every entry in a collection, with a value of type ObjectID
, so that I can sort on that?
Or if not, what would the recommendations be for 'renaming' the _id
field to say tweetId
and then making the _id
field of type ObjectID
Thanks