Closed hellocodeM closed 9 years ago
Sorry I was gone over the holidays! I'm pretty sure this works, could you give me an exact code example of what failed? So I can double check on my end?
On Sat, Dec 27, 2014 at 3:17 AM, 王欢明 notifications@github.com wrote:
I try to query on _id, stored in ObjectId but query in string. It seems that the client could not support it, is that true?
— Reply to this email directly or view it on GitHub https://github.com/amark/mongous/issues/37.
Thank you for reply.
I wrote these code in my program:
db('news.resource.article').find({}, 1, function(res) {
var doc = res.documents[0];
db('news.resource.article').find({_id: doc._id.toString()}, function(reply) {
console.log(reply);
});
db('news.resource.article').find({_id: doc._id}, function(reply) {
console.log(reply);
});
});
The first query could not work but the second works as my expectation. I have this requirement because I use _id as the key of the relation and I need to query it in URL, so it have to be a string. I have tried to build an ObjectID from string with your program 'objectid.js' but it failed too.
Right now I solved this problem in the way that adding another key to the relation instead of _id. Maybe I misunderstand the usage of _id but I try to reduce the use of space so that I use _id as the foreign key.
I am a novice to node.js, and thank you once again for your help. : )
I'm glad you found a work around. Hopefully this isn't too late, I've been awfully busy with my other database that is easier than MongoDB, if you're curious check out http://github.com/amark/gun .
Here is an UGLY quick fix:
var objectID = require("mongous/bson/objectid").ObjectID;
objectID( doc._id.toString() );
This will convert a string version of an _id into an ObjectID... sorry that this didn't work for you earlier. It should now. Copy and paste this, and run it, and tell me if it works:
var db = require("mongous").Mongous;
var objectID = require("mongous/bson/objectid").ObjectID;
db('news.resource.article').save({title: 'hi', body: 'this is an article.'});
db('news.resource.article').find({}, 1, function(res) {
var doc = res.documents[0];
db('news.resource.article').find({_id: objectID(doc._id.toString())}, function(reply) {
console.log(reply);
});
db('news.resource.article').find({_id: doc._id}, function(reply) {
console.log(reply);
});
});
When I ran it, I got the following output:
node testmongous.js
connecting...
connected!
{ documents:
[ { _id: 54b7916c7a1d77e919a01b2e,
title: 'hi',
body: 'this is an article.' } ],
requestId: 53,
responseTo: 54703,
responseFlag: 8,
cursorID: { low_: 0, high_: 0 },
startingFrom: 0,
numberReturned: 1 }
{ documents:
[ { _id: 54b7916c7a1d77e919a01b2e,
title: 'hi',
body: 'this is an article.' } ],
requestId: 54,
responseTo: 33507,
responseFlag: 8,
cursorID: { low_: 0, high_: 0 },
startingFrom: 0,
numberReturned: 1 }
I hope this is satisfactory. Sorry it does not automatically convert them into ObjectIDs! This is definitely a feature mongous should have. However, since I'm building a better database, I probably won't be adding that feature. I'd love it if somebody else did though!
Please let me know if this fixes your issue, albeit a little ugly.
I add few lines to your code to let it automatically convert _id which type is string to ObjectID, using Regex '^[0-9a-f]{24}'. The following code works as expected:
db('news.resource.article').find({_id: doc._id.toString()}, function(reply) {
console.log(reply.documents[0].title);
});
db('news.resource.article').find({_id: doc._id}, function(reply) {
console.log(reply.documents[0].title);
});
If you don't mind, you could use it.
By the way, there are some many staff like a, b, c in your code, have you compressed it ? It's really difficult for me to read it.
Thank you so much!!! This is fantastic! I love contributions. I'm headed off to a hackathon, but once I get back next week I'll accept this and pull it into the code! Thank you!
Sorry about the a,b,c stuff, I'm a mathematician so I'm used to having one letter variable - it is a bad habit of mine. We'll talk more when I'm back. Thanks!
checking this out now!
DONE!!! Thanks so much, this is now on 0.2.7 version! :)
Closing, anything else I can help you with? Or anything else you want to contribute?
Oh, over the past few days I'm working on my final exam, so I forgot to check my Github. I'm sorry about this. It's my first time to contribute to others's project, it's really cool! Recently, I intend to learn some other things, like Scala, Spark, 《SICP》, etc. Of course, If anything I could help you, I'm willing to do this. Bye.
No problem at all! We're all busy!
I'm excited mongous was your first contribution! :) Thank you so much.
If you are interested in functional, immutable, and graph based data ideas, hearing your feedback on https://github.com/amark/gun would be great!
Thanks again.
I try to query on _id, stored in ObjectId but query in string. It seems that the client could not support it, is that true?