Get the _id of inserted document in Mongo database in NodeJS - javascript

I use NodeJS to insert documents in MongoDB. Using collection.insert I can insert a document into database like in this code:
// ...
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId; // = ???
});
// ...
How can I get the _id of inserted object?
Is there any way to get the _id without getting latest object inserted _id?
Supposing that in same time a lot of people access the database, I can't be sure that the latest id is the id of object inserted.

A shorter way than using second parameter for the callback of collection.insert would be using objectToInsert._id that returns the _id (inside of the callback function, supposing it was a successful operation).
The Mongo driver for NodeJS appends the _id field to the original object reference, so it's easy to get the inserted id using the original object:
collection.insert(objectToInsert, function(err){
if (err) return;
// Object inserted successfully.
var objectId = objectToInsert._id; // this will return the id of object inserted
});

There is a second parameter for the callback for collection.insert that will return the doc or docs inserted, which should have _ids.
Try:
collection.insert(objectToInsert, function(err,docsInserted){
console.log(docsInserted);
});
and check the console to see what I mean.

As ktretyak said, to get inserted document's ID best way is to use insertedId property on result object. In my case result._id didn't work so I had to use following:
db.collection("collection-name")
.insertOne(document)
.then(result => {
console.log(result.insertedId);
})
.catch(err => {
// handle error
});
It's the same thing if you use callbacks.

I actually did a console.log() for the second parameter in the callback function for insert. There is actually a lot of information returned apart from the inserted object itself. So the code below explains how you can access it's id.
collection.insert(objToInsert, function (err, result){
if(err)console.log(err);
else {
console.log(result["ops"][0]["_id"]);
// The above statement will output the id of the
// inserted object
}
});

if you want to take "_id" use simpley
result.insertedId.toString()
// toString will convert from hex

Mongo sends the complete document as a callbackobject so you can simply get it from there only.
for example
collection.save(function(err,room){
var newRoomId = room._id;
});

You could use async functions to get _id field automatically without manipulating data object:
async function save() {
const data = {
name: "John"
}
await db.collection('users').insertOne(data)
return data
}
Returns (data object):
{
    _id: '5dbff150b407cc129ab571ca',
    name: 'John',
}

Now you can use insertOne method and in promise's result.insertedId

#JSideris, sample code for getting insertedId.
db.collection(COLLECTION).insertOne(data, (err, result) => {
if (err)
return err;
else
return result.insertedId;
});

Similar to other responses, you can grab the variable using async await, es6+ features.
const insertData = async (data) => {
const { ops } = await db.collection('collection').insertOne(data)
console.log(ops[0]._id)
}

Another way to do it in async function :
const express = require('express')
const path = require('path')
const db = require(path.join(__dirname, '../database/config')).db;
const router = express.Router()
// Create.R.U.D
router.post('/new-order', async function (req, res, next) {
// security check
if (Object.keys(req.body).length === 0) {
res.status(404).send({
msg: "Error",
code: 404
});
return;
}
try {
// operations
let orderNumber = await db.collection('orders').countDocuments()
let number = orderNumber + 1
let order = {
number: number,
customer: req.body.customer,
products: req.body.products,
totalProducts: req.body.totalProducts,
totalCost: req.body.totalCost,
type: req.body.type,
time: req.body.time,
date: req.body.date,
timeStamp: Date.now(),
}
if (req.body.direction) {
order.direction = req.body.direction
}
if (req.body.specialRequests) {
order.specialRequests = req.body.specialRequests
}
// Here newOrder will store some informations in result of this process.
// You can find the inserted id and some informations there too.
let newOrder = await db.collection('orders').insertOne({...order})
if (newOrder) {
// MARK: Server response
res.status(201).send({
msg: `Order N°${number} created : id[${newOrder.insertedId}]`,
code: 201
});
} else {
// MARK: Server response
res.status(404).send({
msg: `Order N°${number} not created`,
code: 404
});
}
} catch (e) {
print(e)
return
}
})
// C.Read.U.D
// C.R.Update.D
// C.R.U.Delete
module.exports = router;

Related

trying to remove an item from a database

Sorry If I'm not explaining this very well. I'm trying to delete from a database using this button press, but whenever I try to delete it will only delete the first item in the database. I'm pretty sure there's something wrong with the ID I'm getting. Can anyone spot any obvious issues I'm missing here?
Button Press:
tasksDOM.addEventListener('click', async (e) => {
const el = e.target
if (el.parentElement.classList.contains('delete-btn')) {
loadingDOM.style.visibility = 'visible'
const id = el.parentElement.dataset.id
try {
await axios.delete(`/tm/v1/tasks/${id}`)
showTasks()
} catch (error) {
console.log(error)
}
}
loadingDOM.style.visibility = 'hidden'
})
Delete:
app.delete("/tm/v1/tasks/:id", async (req, res) => {
try {
const id = req.params.id;
const response = await Task.findOneAndDelete({ id });
res.status(200).json({ msg: 'deleted' });
} catch (error) {
res.status(500).json({ msg: error });
};
});
Two solutions -
1. const response = await Task.findByIdAndDelete(id);
2. const response = await Task.findOneAndDelete({ _id : mongoose.Types.ObjectId(id) });
Don't forget to import mongoose for second method.
Explanation -
In your code findOneAndDelete is taking id as argument which doesn't exist for mongoose so default, its deleting the first entry so you need to use _id here. Second thing is, id param is string type and _id is ObjectId so it will not match. To match this, you need to convert this string value to mongoose ObjectId.
findByIdAndDelete works with string value as well!

Error while deleting a value of element in mongoDB array using filter function?

I tried to find the solutions over here but unable to get success while using $pull as the array values I have does not contain `mongo_id'.
So the scenario is that , I am trying to delete the specific comment of the particular user which I am passing through query params. M
My mongo data looks like this:
Now I am making API Delete request like this : http://localhost:8000/api/articles/learn-react/delete-comment?q=1 on my localhost .
ANd finally my code looks like this:
import express from "express";
import bodyParser from "body-parser";
import { MongoClient } from "MongoDB";
const withDB = async (operations, res) => {
try {
const client = await MongoClient.connect(
"mongodb://localhost:27017",
{ useNewUrlParser: true },
{ useUnifiedTopology: true }
);
const db = client.db("my-blog");
await operations(db);
client.close();
} catch (error) {
res.status(500).json({ message: "Error connecting to db", error });
}
};
app.delete("/api/articles/:name/delete-comment", (req, res) => {
const articleName = req.params.name;
const commentIndex = req.query.q;
withDB(async(db) => {
try{
const articleInfo = await db.collection('articles').findOne({name:articleName});
let articleAllComment = articleInfo.comments;
console.log("before =",articleAllComment)
const commentToBeDeleted = articleInfo.comments[commentIndex];
//console.log(commentToBeDeleted)
// articleAllComment.update({
// $pull: { 'comments':{username: commentToBeDeleted.username }}
// });
articleAllComment = articleAllComment.filter( (item) => item != commentToBeDeleted );
await articleAllComment.save();
console.log("after - ",articleAllComment);
//yaha per index chahiye per kaise milega pta nhi?
//articleInfo.comments = gives artcle comment
res.status(200).send(articleAllComment);
}
catch(err)
{
res.status(500).send("Error occurred")
}
},res);
});
I have used the filter function but it is not showing any error in terminal but also getting 500 status at postman.
Unable to figure out the error?
I believe you'll find a good answer here:
https://stackoverflow.com/a/4588909/9951599
Something to consider...
You can use MongoDB's built-in projection methods to simplify your code.
https://docs.mongodb.com/manual/reference/operator/projection/positional/#mongodb-projection-proj.-
By assigning a "unique ID" to each of your comments, you can find/modify the comment quickly using an update command instead of pulling out the comment by order in the array. This is more efficient, and much simpler. Plus, multiple read/writes at once won't interfere with this logic during busy times, ensuring that you're always deleting the right comment.
Solution #1: The recommended way, with atomic operators
Here is how you can let MongoDB pull it for you if you give each of your comments an ID.
await db.collection('articles').updateOne({ name:articleName },
{
$pull:{ "comments.id":commentID }
});
// Or
await db.collection('articles').updateOne({ name:articleName, "comments.id":commentID },
{
$unset:{ "comments.$":0 }
});
Solution #2 - Not recommended
Alternatively, you could remove it by index:
// I'm using "3" here staticly, put the index of your comment there instead.
db.collection('articles').updateOne({ name:articleName }, {
$unset : { "comments.3":0 }
})
I do not know why your filter is erroring, but I would recommend bypassing the filter altogether and try to utilize MongoDB's atomic system for you.

MongoDB findOne funtion returns null when comparing with id

I am using the following code to get the details of a user when I pass their id as a parameter:
server.get("/users/:id", (req, res) => {
const itemId = req.params.id;
dbCollection.findOne({ _id: itemId }, (error, result) => {
if (error) throw error;
// return item
res.json(result);
});
});
However, this doesn't seem to work, as whenever I run the code, I get returned with a null value.
My question is not the same as many previously asked questions because I have ObjectId('randomId') as my id, and not a string. How can I fix my code?
req.params.id comes as a string while your _id is an ObjectId so this won't work since MongoDB requires type equality first, you need to cast the value:
const itemId = mongoose.Types.ObjectId(req.params.id);
MongoDB wouldn't consider the "itemId" as a MongoDB id, therefore you need to transform it as shown below:
new mongodb.ObjectId(itemId)
This implies that:
const mongodb = require('mongodb')
As others already said, MongoDB expects _id to be an ObjectID. But if you are searching for ONE item, instead of using findOne use findById, which accepts id as a string.
const { Model } = require('./models'); // Model is your Mongoose Schema
server.get("/users/:id", async (req, res) => {
const { id } = req.params;
// This is the same as Model.findOne({ _id: new ObjectId(id) });
const item = await Model.findById(id);
return res.json(item);
});

After Summing Amounts Getting 'Converting circular structure to JSON' Error in MongoDB [duplicate]

I have some code that pulls all documents from a collection and puts it onto a webpage. a simplified version looks like this:
var mongodb = require("mongodb"),
express = require("express"),
mongoServer = new mongodb.Server('localhost', 27017),
dbConnector = new mongodb.Db('systemMonitor', mongoServer),
db;
var app = new express();
app.get('/drives', function(req, res) {
db.collection('driveInfo', function(err, collection) {
if (err) throw err;
collection.find({}, function(err, documents) {
res.send(documents);
});
});
});
dbConnector.open(function(err, opendb) {
if (err) throw err;
db = opendb;
app.listen(80);
});
I have a driveInfo collection which contains a long list of documents. Each document contains nested objects. What I would like to do, is whenever someone visits /drives in their browser, to print the entire collection as a json object so that I can grab everything with jquery later (beginnings of an api)
However, I get an error saying "TypeError: Converting circular structure to JSON". The error on the page points to this line of code:
collection.find({}, function(err, documents) {
res.send(documents);
});
I'm unsure what the problem is, or where the self-reference is. Am I not querying the collection properly?
Not sure what version of the API you are using, but i think that your syntax might be wrong looking at the API spec:
http://docs.mongodb.org/manual/reference/method/db.collection.find/
This is the declaration:
db.collection.find(<criteria>, <projection>)
And you are definitely misusing the projection parameter. Passing a callback like you are doing seems to return the db object in the result, which is causing the circular error during JSON serialization in express.
The correct code for the find all operation should be something like:
collection.find({}).toArray(function(error, documents) {
if (err) throw error;
res.send(documents);
});
In my case I was getting the error because I was querying(using mongoose find method) without doing an await. Please see below
Query that gave the error (as I haven't executed this query using await) :
const tours = Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
Error that I got on postman due to this :
"message": "Converting circular structure to JSON\n --> starting at object with constructor 'NativeTopology'\n | property 's' -> object with constructor 'Object'\n | property 'sessionPool' -> object with constructor 'ServerSessionPool'\n --- property 'topology' closes the circle"
How I got rid of the above error (added await) :
const tours = await Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
callback option is from Mongoose not from MongoDB see docs.
// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// Example
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
Look that the response is into callback that in your case it would be
YourModel.find({}, function(err, documents) {
if(err) return done(err);
res.send(documents); // <-- here
});
// <-- not here
In Mongo there is a cursor method to access the documents next() see docs :
var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
var myName = myDocument.name;
print (tojson(myName));
}
You can find CRUD operations in mongo docs at manual/crud. In Query Documents you will see db.inventory.find( {} ) : To select all documents in the collection, pass an empty document as the query filter parameter to the find method.
Async/Await function solution : Mongo Docs
app.get( '/api/users' , async (req,res)=>{
const getUsers = await NewUser.find({});
res.json( getUsers );
})
< callback > solution : Mongoose Docs.
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
const res1 = await db.collection("some-db").find()
Here, res1 will contain a "cursor" which has a circular structure, hence the given error is thrown.
Try adding const res2 = await res1.toArray() to the code.
Here, res2 will now contain an array of documents, pointed by cursor res1, which is the documents you were querying for.

mongodb nodejs - converting circular structure

I have some code that pulls all documents from a collection and puts it onto a webpage. a simplified version looks like this:
var mongodb = require("mongodb"),
express = require("express"),
mongoServer = new mongodb.Server('localhost', 27017),
dbConnector = new mongodb.Db('systemMonitor', mongoServer),
db;
var app = new express();
app.get('/drives', function(req, res) {
db.collection('driveInfo', function(err, collection) {
if (err) throw err;
collection.find({}, function(err, documents) {
res.send(documents);
});
});
});
dbConnector.open(function(err, opendb) {
if (err) throw err;
db = opendb;
app.listen(80);
});
I have a driveInfo collection which contains a long list of documents. Each document contains nested objects. What I would like to do, is whenever someone visits /drives in their browser, to print the entire collection as a json object so that I can grab everything with jquery later (beginnings of an api)
However, I get an error saying "TypeError: Converting circular structure to JSON". The error on the page points to this line of code:
collection.find({}, function(err, documents) {
res.send(documents);
});
I'm unsure what the problem is, or where the self-reference is. Am I not querying the collection properly?
Not sure what version of the API you are using, but i think that your syntax might be wrong looking at the API spec:
http://docs.mongodb.org/manual/reference/method/db.collection.find/
This is the declaration:
db.collection.find(<criteria>, <projection>)
And you are definitely misusing the projection parameter. Passing a callback like you are doing seems to return the db object in the result, which is causing the circular error during JSON serialization in express.
The correct code for the find all operation should be something like:
collection.find({}).toArray(function(error, documents) {
if (err) throw error;
res.send(documents);
});
In my case I was getting the error because I was querying(using mongoose find method) without doing an await. Please see below
Query that gave the error (as I haven't executed this query using await) :
const tours = Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
Error that I got on postman due to this :
"message": "Converting circular structure to JSON\n --> starting at object with constructor 'NativeTopology'\n | property 's' -> object with constructor 'Object'\n | property 'sessionPool' -> object with constructor 'ServerSessionPool'\n --- property 'topology' closes the circle"
How I got rid of the above error (added await) :
const tours = await Tour.find({
startLocation: {
$geoWithin: { $centerSphere: [[longitude, latitude], radius] }
}
});
callback option is from Mongoose not from MongoDB see docs.
// Mongoose Docs : callback option
MyModel.find({ name: 'john', age: { $gte: 18 }}, function (err, docs) {});
// Example
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
Look that the response is into callback that in your case it would be
YourModel.find({}, function(err, documents) {
if(err) return done(err);
res.send(documents); // <-- here
});
// <-- not here
In Mongo there is a cursor method to access the documents next() see docs :
var myCursor = db.bios.find( );
var myDocument = myCursor.hasNext() ? myCursor.next() : null;
if (myDocument) {
var myName = myDocument.name;
print (tojson(myName));
}
You can find CRUD operations in mongo docs at manual/crud. In Query Documents you will see db.inventory.find( {} ) : To select all documents in the collection, pass an empty document as the query filter parameter to the find method.
Async/Await function solution : Mongo Docs
app.get( '/api/users' , async (req,res)=>{
const getUsers = await NewUser.find({});
res.json( getUsers );
})
< callback > solution : Mongoose Docs.
app.get( '/api/users' , (req,res,done)=>{
let getUsers = NewUser.find({},(err,data)=>{
if(err) return done(err);
res.json(data)
});
});
const res1 = await db.collection("some-db").find()
Here, res1 will contain a "cursor" which has a circular structure, hence the given error is thrown.
Try adding const res2 = await res1.toArray() to the code.
Here, res2 will now contain an array of documents, pointed by cursor res1, which is the documents you were querying for.

Categories