I have stored crypto account addresses in mongodb, and want to retrieve documents using the addresses. One example look like this "0x754B2f9797b096f417a24686c4B368F508...". My schema specify the addresses are string, and thus, the addresses are stored in mongodb in string and look as the above example. I can retrieve document as expected in mongosh (shell). However, I cannot retrieve any document even if I pass literal string in the filter with mongoose (using find, findOne, and so on). The only way to retrive documents is to not pass any filter. I guess mongoose interprets the value as number and thus convert it before pass to mongodb.
How can I fix the problem?
Thanks.
Related
I have a Mongodb Collection in which a decimal amount is stored as a string.
I would like to be able to query treating that field as a number.
Is that possible without elaborate code? I would expect to be able to do. tonumber(field):{$gt:100}.
But I can't find anything.
I'm starting to play with mongodb, and I learned that when inserting a document, you can either provide an ID, or let mongodb generate it for you.
I thought this is nice, because I want to let my users optionally choose an id, and if not generate it for them.
But the problem is, the generated one is of type ObjectId while the user provided one is a string, and the find method only returns the correct answer if you pass it with the correct type. So when a user requests GET /widget/123, I have no idea if the original ID was stored as an ObjectId or a string do I?
So how am I supposed to use this feature?
First off, I'd recommend against letting users provide _ids: if 2 users want to use the same _id, the second user will be unable to, which will be frustrating. If users want that functionality, I'd recommend storing the user created id on a separate field & querying by user (or company or whatever) and the user-created id.
That said, mongo ObjectIds are 24 hex characters, so you can safely identify when an id is not a MongoId by checking whether it doesn't match /^[a-f0-9]{24}$/ (or by seeing whether a call to ObjectId("maybeAnObjectId") throws). In the case where it's unclear (where a user might have provided 24 hex characters as their id), you'll need to use $in (or $or) to query for both cases:
const query = /^[a-f0-9]{24}$/.test(id) ? { _id: {$in: [ObjectId(id), id]}} : {_id: id}
(an annoying user could re-use an autogenerated ObjectId as their string id, and then queries to that route would return two values and there'd be no way of differentiating them).
I've to filter the document keys for different users based on the user roles/permissions.
For example, 1 user can get secretCode while others not.
Converting the document to an object does convert the doc to a plain object but the ObjectIds are still objects (bson type).
I can try _id.toString() but I've multiple fields with the objectId. So it will be kind of hard-coding in all the fields. Plus, I'll also have to validate if the value is not null, else .toString() is not a function will pop up.
Another way is to use JSON.parse(JSON.stringify(obj)) but this synchronous and unnecessary computation.
Any suggestions?
I want to create some APIs, which let user pass in just strings for types like number, boolean. And automatically convert them before querying mongodb. Is it possible?
Yes, It is possible for MongoDB. You could write your own utility to convert string in mongo specific query or you could use some open source utilities like enter link description here.
Eventually, MongoDB accepts JSON string to execute the same client does also convert each query in the same JSON format. MongoDB clients or MongoDB doesn't need any predefined mapping or POJO.
This utility will convert string as shown below -
User string -
"select * from users where firstName='Vijay' AND lastName='Rajput'"
Then this utility will convert it into -
db.users.find({$and: [{firstName: 'Vijay'}, {lastName: 'Rajput'}]})
I have a Meteor application whereby I initially use the _id field from each record in my collection when naming list items in my template.
When get the _id field, I convert it to a string to use in the template.
Now I want to update these records in Mongo and am passing the _id back to a Meteor.method, but these are still in string format and Mongo is expecting an ObjectID(). Is there a simple way to convert this string to the ObjectID()? If not, what alternatives do I have?
Ok, found it! On the /server, within your Meteor method function do this to convert it:
var mid = new Mongo.ObjectID(str_id_sent_to_server);