How can I find object ID of MMT4 by nodeJS and mongoDB ?
Thank you for your help ❤️
Hi I worked on your question and if you are using mongodb and nodejs you can do the following stpes:
using $match and $unwind aggregations to find the object that match with title MMT4:
db.collection.aggregate([{$unwind: "$children"}, {$match: {"children.title": "MMT4"}}])
Notice: I wrote the above syntax in mongodb shell you can write it in JS with mongoose library.
the result is an object with children array that contain just MMT4 object. you can iterate in this single element array using JS to get _id of the MMT4
reference: click here
Related
I am trying to query an old mongoDB collection in node.js with the native driver.
It uses Legacy UUID as its _id field.
In my code I am using Binary values such as: "2u0kLwUZuEWQvjqOjgQU4g=="
and I want to query the collection with find() operation.
When trying to test it directly with mongoDB I am able to use the following query:
db.getCollection('myCollection').find({_id: BinData(3, "2u0kLwUZuEWQvjqOjgQU4g==")})
to find my item.
But what I need to do is to find multiple items.
So I need to use something like this:
db.getCollection('myCollection').find({_ids: { $in: [BinData(3, "2u0kLwUZuEWQvjqOjgQU4g=="), BinData(3, "3u0kLwUZuEWQvjqOjgQU4g==")...]}})
but it does not seem to work and always returns zero records.
I am not sure why? and what might be the correct way to query multiple Legacy UUIDs?
I'm using Mongodb Driver for Node and I'm defining an _id property manually using Nanoid package (which creates an string id) to a specific collection.
Typescript is getting mad with me when I try to create a new document because Mongodb Driver defines that the _id property needs an ObjectId type, but it is getting a Nanoid string.
Anyone knows how to make typescript happy at this situation?
I'm trying to set raw query from node to mongodb with mongoose driver.
I got the query from user using javascript
var keys = $('#advance-search').val();
then I Post the keys to the node server and trying to using the query in find() like the following:
var search = req.query.keys || ""
Product.find(keys)...
the problem is the type of keys is String and find() need object
when I tried to convert keys to object by keys = JSON.parse(keys) I got exception because the mongo query not a valid Json format
example: {'product.price' :{ $in :[500,400,300,600]}}
so what is the best way to make a raw query from html to mongo by node js, and how I can filter this query before execution?
Is there a way to tell the native MongoDB driver for NodeJS to automatically convert the contents of an _id field into an ObjectID?
Say, in this situation:
db.collection("collection").updateOne({_id: data._id}, data)
It's not that data._id = ObjectID(data.id) is hard, but it's another thing to miss each and every time.
There is no way to do that natively. You can make some function for wrapping your mongo queries where you will check params and if it's "_id" parse it to ObjectId.
I have a document:
{
"_id":ObjectId("someID")
"email":"john#example.com"
}
I would like to retrieve "john#example.com" so that I can pass this value to the browser.
How do I do that?
I saw these SO posts, but I don't understand the solution at all:
How to get string value inside a Mongodb document?
How to get value from a MongoDB document