I need to update the data from client side in mongodb but I can see clicked updated Id value in node js but it does not update in mongodb wat to do.. help me to update the values in mongo db using id values
router.post('/datapassfup', (req, res) => {
console.log("updated values are",req.body)
MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("mohan");
var myquery = { id: req.body.id };
var newvalues = { $set: {name: req.body.name, username:
req.body.username } };
dbo.collection("customers").updateMany(myquery,newvalues,
function(err, res) {
if (err) throw err;
console.log("1 document updated");
db.close();
});
});
});
if you use the mongodb id for you query then you need to create a new objectid for _id search
const {ObjectId} = require("mongodb");
const query = {_id:new ObjectId(req.body.id)}
Related
In mongodb i have a database schema like this :
_id : ObjectId('123456')
name: "abc"
number:"1"
i using this nodejs code so when enter the name i received the whole data above:
app.post("/showid", (req, res) => {
let name = req.body.name;
MongoClient.connect(uri, function (err, db) {
if (err) throw err;
var dbo = db.db("mydb");
dbo
.collection("mycollection")
.find({ name :name })
.toArray(function (err, result) {
if (err) throw err;
res.json(result);
db.close();
});});});
i still received an array of the data i want inside , but when i changed name to id ,
find({ _id: id })
and i enter the string inside ObjectId , i received an empty array . What did i do wrong ?
When i Try to push key:value pair with the following code, it inserts data as object instead of key value pair. Following is the code im using to insert data into MongoDb
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("Test");
var myquery = { userId: "test#gmail.com" };
// var newvalues = { $set: { name: "Mickey", address: "Canyon 123" } };
var newvalues = {
$push: { questionAnsId: { $each: [{ wk: 5 }] } }
}
dbo.collection("Forms").updateOne(myquery, newvalues, function (err, res) {
if (err) throw err;
console.log("1 document updated");
db.close();
});
});
Upon inserting data, this is what im getting
enter image description here
instead of saving data in
questionAnsId: {Object}
i want to save it as
questionAnsId:{key:value, key:value}
Any Idea how to solve this ?
I would like to upload a file from html form using an api written in node js.
Following code is the api to upload the file ,But i am not able to get the exact query for saving a file as attachment in the db.
server.post('/upload/', passport.authenticate('oauth-bearer', {
session: false
}), (req, res, next) => {
if(!req.body.userId || !req.body._id ) {
return res.send({"message":"missingParameter","statuscode":"404"});
}
else {
// // Retrieve
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://localhost:27017/", function(err,client) {
var ObjectId = require('mongodb').ObjectId;
if (err) throw err;
var dbo = client.db("Db");
}
}
}
//below is the query to insert data .How to insert a file using mongodb query into cosmos db.
var data = { "_id" : ObjectId(req.body._id) ,"file":req.body.files};
dbo.collection("uploads").insert(data, function(err, result) {
if (err) throw err;
console.log("1 document inserted");
client.close();
return res.send({"message":"success"});
});
//
});
}
});
I am not able to insert data into MongoDB database using insert method in Nodejs.
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
docs.insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
})
db.close();
}
There is no output coming and connection to the data base is successful but no insertion of data is happening.
Try this will work for you .
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, dbobj) {
if (err) throw err;
// db gives access to the database
// const myDb = dbobj.db('learnyoumongo').collection('docs');
// var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
dbobj.db('learnyoumongo').collection('docs').insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
dbobj.close();
})
})
I figured out what the problem is. Seems like I forgot to add a parenthesis in the end:
var url = 'mongodb://localhost:27017/learnyoumongo';
var mongo = require('mongodb').MongoClient;
mongo.connect(url, function(err, db) {
if (err) throw err;
// db gives access to the database
const myDb = db.db('learnyoumongo');
var docs = myDb.collection('docs');
var obj = {firstName: process.argv[2], lastName: process.argv[3]};
docs.insert(obj, function(err, res){
if(err) throw err;
console.log('data inserted');
})
db.close();
})
You can use the following code to insert data into MongoDB using nodejs.
var mongo = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/learnyoumongo";
mongo.connect(url, function(err, db) {
if (err) throw err;
//access to the database
var myobj = { firstname: "Jhon", lastname: "Mackey" };
db.collection("docs").insertOne(myobj, function(err, res) {
if (err) throw err;
console.log("Data inserted");
db.close();
});
});
I'm trying to get the result of query but i get the same info in all vars: db, collection and res:
var mongodb = require("mongodb");
var mongoserver = new mongodb.Server("localhost", 27017);
var instance = new mongodb.Db("test", mongoserver);
instance.open(function(err, db)
{
console.log('db:');
console.log(db);
db.collection('kurtsoa', function(err, collection)
{
console.log('collection:');
console.log(collection);
collection.find({}, function(err, res)
{
console.log('res:');
console.log(res);
});
});
});
how i can get the result of "find"?
.find() will return a Cursor object for you to work with. If all you are interested in is getting all the results in an array you can do:
collection.find().toArray(function(err, docs) {
console.log(docs);
});
But you can also iterate the cursor too:
collection.find().each(function(err, doc) {
//called once for each doc returned
});
You can use this:
collection.find().toArray(function(err, docs){
console.log(docs);
)};