In mongodb there is a document like below,
{
"_id": ObjectId("57443657ee5b5ccc30c4e6f8"),
"name": "Kevin",
"email": "kevinwarn#gmail.com",
"password": "$2a$13$iZ0PhuY6VlBF6qC9rUredrG39Fw5pEKkIh.VCLiGsLPZMKYveyzey",
"mobile": "9980896745",
"__v": NumberInt(0),
"ocassionTypes": [
{
"occasiontype": "Anniversary",
"date": "2016-05-30T18:30:00.000Z"
},
{
"occasiontype": "Donation",
"date": "2016-07-24T18:30:00.000Z"
},
{
"occasiontype": "House Warming",
"date": "2016-09-21T18:30:00.000Z"
}
]
}
So I have written a query in Nodejs to search occasiontype element in ocassionTypes array like below,
router.post('/find-registry', function(req, res){
var uEmail = req.body.email;
var uocType = req.body.userOccasion;
var findUserId = function(db, callback) {
var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}}).toArray(function(err, docs1){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs1);
}
});
};
MongoClient.connect(config.database, function(err, db) {
assert.equal(null, err);
findUserId(db, function(err,docs1) {
db.close();
if(err) return res.json({result:null})
else
return res.json({result1:docs1});
});
});
});
Using this query I am getting 0th index element, but if I give 1st and 2nd element it always shows only 0th index in the output.
In front end I have given input as shown in the picture below.
file.html
Is there any wrong in my query? please help me to fix this.
your query is right but it will give matched document with full array
just add projection in your query
db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email:1, ocassionTypes: {$elemMatch: {occasiontype:uocType}}})
If you are searching in sub document, mongodb returns all sub-document instead of matched sub-document. You can limit no. of sub-document using following code.
var cursor =db.collection('users').find({email:uEmail, ocassionTypes: {$elemMatch: {occasiontype:uocType}}},{email: 1, ocassionTypes: {$elemMatch: {occasiontype: uocType}}}).toArray(function(err, docs1){
if(err){
callback(new Error("Some problem"));
} else {
callback(null,docs1);
}
});
It is not tested but it should work.
Related
I am saving an updated array of _id's to an array on a document. If I check the document, everything is updating quite nicely, but when I populate right afterward using a new find query, it doesn't work right. populate wasn't working for me, so I decided to handle it manually and I am successful at filling them in, but they're in the wrong order.
export function sort_update(req, res) {
Action.findOneAndUpdate({
_id: req.query.action
}, {
child_actions: req.body.newArray
}, {
useFindAndModify: false, new: true
}, function(err, doc) {
if (err) return res.send(500, {
error: err
});
console.log("child action id strings after update", doc.child_actions); // ["1", "2"] correct!
Action.find({'_id': {$in: doc.child_actions }}, function(err, child_action_docs) {
if (err) return res.send(500, {
error: err
});
console.log("child action objects after fill", child_action_docs); // [{ _id: "2" }, { _id: "1" }] wrong!
doc.child_actions = child_action_docs;
res.send(doc);
});
});
}
Why are they in the wrong order? Does {'_id': {$in: doc.child_actions }} simply not guarantee any order at all? Do I have to re-sort my objects to the doc.child_actions order deliberately or is there a simpler way of telling mongodb to keep the order in the original array?
I have a timeout with my application-level join between two documents : Thread and Message.
I am trying to get all the Messages of my Thread with this code:
router.get('/:themeId/threads/:threadId/messages', function(req, res, next) {
Thread.findById(req.params.threadId, function(err, thread) {
if (err) return next(err);
Message.find({ _id: { $in: thread.messages } }), function(err, message) {
if (err) return next(err);
res.json(message);
}
});
});
Unfortunately, I got a timeout with my request which I tested Postman.
I had this message when I tested it:
Could not get any response
There was an error connecting to
http://localhost:3000/api/themes/5b1bb59d4210c50cf798da57/threads/5b1bb5e84210c50cf798da59/messages.
Also I checked that thread.messages is an array with another request:
GET http://localhost:3000/api/themes/5b1bb59d4210c50cf798da57/threads/5b1bb5e84210c50cf798da59 :
The result is below and from my Threaddocument :
{
"numberOfViews": 0,
"numberOfComments": 0,
"numberOfLikes": 0,
"numberOfThanks": 0,
"messages": [
"5b1bb5ad4210c50cf798da58",
"5b1bb6464210c50cf798da5a"
],
"_id": "5b1bb5e84210c50cf798da59",
"theme": "5b1bb59d4210c50cf798da57",
"title": "Title azerty",
"createdAt": "2018-06-09T11:11:36.358Z",
"updatedAt": "2018-06-09T11:13:41.062Z",
"__v": 1
}
From my understanding, my request should do a find Message in my array of Thread.messages...
But it seems I miss something.
Any idea to solve my timeout request?
Thanks a lot for your help.
Couple of suggestions to try out:
Make sure thread.messages is not an empty array. You can do this by asserting that its length is greater than 0.
In the clause Message.find({ _id: { $in: thread.messages } }), cast the string values to objectids by before sending it to the find clause. So you can do
var myobj1 = [];
thread.messages.forEach(element => {myobj1.push(Mongoose.Types.ObjectId(element)})
I found a solution with the populate of Mongoose and theses sources :
http://mongoosejs.com/docs/populate.html
https://alexanderzeitler.com/articles/mongoose-referencing-schema-in-properties-and-arrays/
Below the modified code:
router.get('/:themeId/threads/:threadId/messages', function(req, res, next) {
Thread.findById(req.params.threadId, function(err, thread) {
if (err) return next(err);
Message.find({_id: { $in : thread.messages } } )
.populate('messages')
.exec(function(error, messages) {
res.json(messages);
});
});
});
The problem is that i can't remove an attachment from an object, in mongoose, with the next schema.
var question=mongoose.Schema({
_id:mongoose.Schema.Types.ObjectId
answers:[{
_id:mongoose.Schema.Types.ObjectId
attachments:[
_id:mongoose.Schema.Types.ObjectId
]
}]
});
I try to remove an attachment from a document with the next code.
Question.update({
_id: req.params.idQuestion,
'answers._id': req.params.idAnswer
}, {
$pull: {
'answers.$.attachments':{_id:req.params.idAttachment}
}
}, function (err, updated) {
if(err){
return res.status(400).send(err);
}
else if(!updated.nModified){
return res.status(400).send('Question hasn\t been updated.');
}
res.send(200);
});
I thought my query weren't correct and tried to do that in mongo shell. It worked perfectly.
db.questions.update({
_id:ObjectId('xx'),
'answers._id':ObjectId('yy')
},{
$pull:{
'answers.$.attachments':{_id:ObjectId('zz')}
}
})
Someone can help me with this problem?
Try this:
var qid=req.params.idQuestion,
aid=req.params.idAnswer;
//find
Question.find({
_id: qid,
'answers._id': aid
},{
answers:1
},function(err,question){
//change
var answers=question.answers.filter(function(el){
return el._id!=aid;
});
//update
Question.update({
_id: qid,
},{$set:{
answers:answers
}},function(err,updated){
...//your callback here
});
});
I am trying to get count of data fetched from the database using find() query in mongoose. Now can anyone tell me can i do something like below or do i have to write other function to do that
merchantmodel.find({merchant_id: merchant_id, rating: {'$ne': -1 }, review: {'$ne': "" }}, {'review':1, '_id':0}, {sort: {time_at: -1}}, function(err, docs) {
if (err) {
} else {
if (docs) {
console.log(docs[1].review);
console.log(docs.size()); // Here by writing something is it possible to get count or not
res.json({success: 1, message : "Successfully Fetched the Reviews"});
}
}
});
Convert returned value to array and then use length property
var query = { merchant_id : merchant_id, rating : { '$ne': -1 }, review: { '$ne': "" }};
var projection = { 'review':1, '_id':0 };
var options = { sort: { time_at: -1 } };
merchantmodel.find(query, projection, options).toArray(function(err, docs) {
if (err) {
throw(err);
}
console.log(docs[1].review);
console.log(docs.length);
res.json({success: 1, message : "Successfully Fetched the Reviews"});
});
You can simply do this:
console.log(docs.length);
The docs variable returned by the find() method is an array so docs.length would do the job.
The mongodb native way to do this would be:
db.collection.find( { a: 5, b: 5 } ).count()
I am using Mongoskin + NodeJS to add new keywords to MongoDB. I want to notify the user that the entry was a duplicate but not sure how to do this.
/*
* POST to addkeyword.
*/
router.post('/addkeyword', function(req, res) {
var db = req.db;
db.collection('users').update({email:"useremail#gmail.com"}, {'$addToSet': req.body }, function(err, result) {
if (err) throw err;
if (!err) console.log('addToSet Keyword.' );
});
});
The result does not seem to be of any use since it doesn't state if the keyword was added or not.
At least in the shell you can differentiate if the document was modified or not (see nModified).
> db.test4.update({_id:2}, {$addToSet: {tags: "xyz" }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.test4.update({_id:2}, {$addToSet: {tags: "xyz" }})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
Update for Node
When you use collection.update(criteria, update[[, options], callback]); you can retrieve the count of records that were modified.
From the node docs
callback is the callback to be run after the records are updated. Has
two parameters, the first is an error object (if error occured), the
second is the count of records that were modified.
Another Update
It seems at least in version 1.4.3 the native Mongo Node driver is not behaving as documented. It is possible to work around using the bulk API (introduced in Mongo 2.6):
var col = db.collection('test');
// Initialize the Ordered Batch
var batch = col.initializeOrderedBulkOp();
batch.find({a: 2}).upsert().updateOne({"$addToSet": {"tags": "newTag"}});
// Execute the operations
batch.execute(function(err, result) {
if (err) throw err;
console.log("nUpserted: ", result.nUpserted);
console.log("nInserted: ", result.nInserted);
console.log("nModified: ", result.nModified); // <- will tell if a value was added or not
db.close();
});
You could use db.users.findAndModify({email:"useremail#gmail.com"},[],{'$addToSet': { bodies: req.body }},{'new':false}). Pay attention to new:false switcher, it allows you to get document before update and you could check whether array contained item before update. However, it could be problematic approach if your documents are big, because you analyze it on client side.
P.S. Your original query with $addToSet is wrong: field name is missing.
Edit: I tried to use count returned by update, but it returns 1 for me in all cases. Here is the code I used for test with MongoDB 2.6:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/mtest', function(err, db) {
if(err) throw err;
db.collection('test').insert({_id:1,bodies:["test"]},function(err,item){
db.collection('test').update({_id:1},{$addToSet:{bodies:"test"}}, function(err,affected){
if(err) throw err;
console.log(affected); //1 in console
});
});
});
i am update a array from Collection with this JSON:
{
"<arrayname>":"<value>"
}
route.js
routes.post("/api/:id", Controller.addOne);
Controller.js
async addOne(req, res) {
//juryman id to list add
if (Object.keys(req.body).length === 1) {
console.log("Size 1");
}
await Session.findOneAndUpdate(
{ _id: req.params.id },
{ $addToSet: req.body }
)
.then(function(success) {
res.send("Successfully saved.");
})
.catch(function(error) {
res.status(404).send(error);
});
},
I have five arrays in my Collection and this changes the JSON array name-value and updates correctly, the respectively Collection array. This works only for one item.