MongoDB / Node.JS: Get attribute value from document - node.js

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

Related

db.collectionsname.findOne({mobilenumber: mobilenumber}).feildname

Im not able to fetch the field value i need only the value of the particular field in node js query, but coming undefined
I need the nodejs query of this mongodb query
Try using projection into find query to return only values you want.
Also don't forget to use await or get the field name into then. And, of course, the query should return any document to get the field.
Check this example which would be similar in Node:
const value = await db.collectionsname.findOne({mobilenumber: mobilenumber},{_id: 0, field: 1}).fieldname
Projection stage is not mandatory but if you only want that value is a good practice not get all document from the DB.

How can I find object ID of MMT4 by nodeJS and mongoDB?

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

Is there a way to return a document right after insertion?

I'm currently trying to return the document I just added to a collection to display it in real time on my homepage.
I followed a solution I found here: link to 2018 solution. But this solution doesn't seem to work for the latest node.js and MongoDb versions.
I'm essentially trying to do the same thing the person in that post was trying to do but I get the following error:
res.json (info.ops[0]);
TypeError: Cannot read property '0' of undefined
Here's my code for reference:
app.post('create-item', (req, res) => {
dB.collection('items').insertOne({text: req.body.text}, (err, info) => {
res.json(info.ops[0]);});});
When we use insertOne with the Nodejs driver 4 (the latest) we get a response like
{"acknowledged" : true, "insertedId" : #object[ObjectId 61105622b633ecabf3706782]}
Save the document to a variable before inserting it.
If the document had _id, check if it was inserted and get the document from the variable.
If the document didn't had _id, check if it was inserted and get the _id also from the response, and add that _id to the document you have in the variable.
Maybe i am missing something but the above is simple to do, if this is what you need.

How can I know the amount of documents modified by monk?

I'm using Monk to connect to to MongoDB from NodeJS.
I am trying to pull an item from an array, and for that purpose I'm using
db.collection('MYCOLLECTION').update({_id:id},{$pull:{reviews:{userId:userId}}, function(err,data){
console.log(data);
})
When I execute this sentence directly in Mongo (through the console), the function returns an object with three properties:
nMatched
nUpserted
nModified
However, when I do the same thing using monk, I am only getting a value '1' which I supposed is the value of the property nMatched. Is there a way for me to get the nModified property?

Unable to assign a random string to _id field.

I am trying to save a document into mongodb using MONK drive.
My object has a field called myid, which is a string of unknown length. I'm very inclined to use this ID as _id of my document but after I assign the value to _id field and save the document, I receive error as below:
Error: Argument passed in must be a single String of 12 bytes or a string of 24
hex characters
Is this some restriction introduced by MONK or MongoDB driver? Is there anyway to workaround this? In mongodb shell, you can use any value as _id;
It is an error thrown by monk. You can override the id function:
collectionName.id = function(str){return str;};
See: GitHub issue

Resources