Last active
August 29, 2015 14:22
-
-
Save khan5v/5628a040e8bba76bbad6 to your computer and use it in GitHub Desktop.
MongoDB query based on ObjectID, getting MongoDB ObjectID from String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# getting an objectId object from a string | |
getObjectID = function(str) { | |
return(mongo.oid.from.string(str)) | |
} | |
database = "" | |
mongo = NULL | |
namespace = "" | |
connectToMongoDB = function(host, db, collection) { | |
database <<- db | |
mongo <<- mongo.create(host=host, db=database) | |
namespace <<- paste(database, collection, sep=".") | |
} | |
papi2 = function(host="122.122.122.122:27017", db="test", collection="requests") { | |
connectToMongoDB(host, db, collection) | |
buf <- mongo.bson.buffer.create() | |
# Let's pretend we want to get all the requests for a certain client, for whom we know his ObjectID | |
mongo.bson.buffer.append(buf, "clientID", getObjectID("5446687c0cf2521ace5e1111")) | |
query <- mongo.bson.from.buffer(buf) | |
cursor <- mongo.find(mongo, namespace, query) | |
success = c() | |
while (mongo.cursor.next(cursor)) { | |
val <- mongo.cursor.value(cursor) | |
# collecting successes | |
success <- c(success, mongo.bson.value(val, "scs")) | |
# might be useful to transform every entry into a list | |
list <- mongo.bson.to.list(val) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment