2

My mongoDB collection has some records like:

{ "_id" : ObjectId("4d99b276368394f5130022fc") }
{ "_id" : ObjectId("4d99b276368394f5130022fd") }
{ "_id" : ObjectId("4d99b276368394f5130022fe") }
{ "_id" : ObjectId("4d99b276368394f5130022ff") }
{ "_id" : ObjectId("4d99b27d368394f613002470") }
{ "_id" : ObjectId("4d99b27d368394f613002471") }
{ "_id" : ObjectId("4d99b27d368394f613002472") }

Since the last bytes of a BSON objectId are more or less sequential,I intend to seperate them in multiple channels , for example "Ending with specific letter of ObjectId".

( equivalent of RLIKE in MySql ). Since objectIds are bytes and not strings, my regex does not seem to work. I tried something like:

 db.myColl.findOne( { "_id" : /b$/ } , { "_id":1} )
null
> db.myColl.findOne( { "_id" : /^4/ } , { "_id":1} )
null

Any suggestions to partition my data based on objectId ? like last letter of objectId or something else which gives more or less equal distribution ?

EDIT: I found one way which worked for me :

db.myColl.findOne({ $where: "this._id.toString()[23] == 1" } ) // gives me records with ObjectId ending with 1 .

Other suggestions are still welcome

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • You can add an extra column to each document that contains a random number between 1 and 100 that you generate in your middle tier. The advantage is that you can index that column. Maybe a stupid answer but I don't understand why you need those channels. – TTT Apr 04 '11 at 19:35
  • That is a good suggestion,but I do not want to add extra data,looking for some way to partition the data on query level using existing fields,i thought last character of objectId would be a good way. – DhruvPathak Apr 06 '11 at 06:02
  • Could refer to this. http://stackoverflow.com/questions/14315684/query-mongodb-using-objectid – Maziz Aug 11 '14 at 14:08

2 Answers2

1

Maybe not exactly what you re looking for, but hopefully very close...

db.mycollection.find({_id: {$gte: new ObjectId("4d99b27d368394f613002472")}})
balafi
  • 2,143
  • 3
  • 17
  • 20
  • How does that help in selecting a part of the data ? My intent is to select a distinct fraction say "1/16" of the records based on ObjectId.( say all ObjectIds ending with 0 ). One more idea is to use getTime on ObjectId and divide based on Seconds. – DhruvPathak Sep 07 '11 at 04:57
0

So if you want to take advantage of the auto-sharding capability in MongoDB, please read the following

http://www.mongodb.org/display/DOCS/Sharding+Introduction

user602502
  • 141
  • 2
  • I am not looking to shard data at database level, I just want to select specific set of data with different instances of same script and process it. eg. 10 instances of same script will fetch 1/10 data of a data set. – DhruvPathak Apr 06 '11 at 06:03