Complex Mongoose findOneAndUpdate query not working looking for specific advice - javascript

I have a somewhat complex Mongoose query I want to make and while I can find the different parts around the internet (including here) I can't seem to find exactly what I need and piecing the existing info together has not worked so I am looking for specific how to do this.
I want to findOneAndUpdate to add data to blockSection array in my document.
Block.findOneAndUpdate({
section: {$elemMatch: {section_code: sectionID}}
},
{"$push": {blockSection.section: blocSecData}}, // blocSecData is defined elsewhere and is an object
(err, result) => {
if(err) {
return res.json({success: false, err})
}
... // more code here to processstuff
})
Rough example of a sample Block with only one item per array
{
"section_code":"<abc>",
"blockSection": [
{
section: [{"text":"hello world"}], // add the data here
"_id":"<foobar>",
"blockSecID":"<a uuid>"
}
]
}

Related

How to insert json object inside nested object in a document based on nested object UUID

I am working on a small project and I am stuck from last month and looking for an exact query to update my document at the exact location...I did my best but didn't get my goal....Please help.
My Current Query
await test.update(
{
"subtests.uuid": req.query.sub_uuid
},
{
$set: {
$push:
{
"subtests.$[element].subtests": req.body
}
}
},
{
arrayFilters: [
{
"element.uuid": req.query.sub_uuid
}
]
});
With this query, I am getting an error saying (Typeerror: Callback.apply is not a function)
MongoDb Document Image
Error
Thank You to everyone who holds my hand in this complex query (for me it very complex because I am trying to solve it from the month)

How to filter an array of subdocuments by two fields in each subdocument

I am attempting to add a help request system which allows the requestor to make only one request for help on each topic from an expert. If the expert lists multiple topics which they can help, I want to limit each requestor to one help request per topic per expert.
I am using node.js and mongoose.js with a self-hosted mongodb instance
I have tried using the $and operator to find the ._id of the expert as long as they don't already have an existing request from the same requestor on the same topic. It works for one update but after the experts document has a subdocument inserted with either the topic_id or the requestor_id the filter is applied and no expert is returned.
// Schema
ExpertSchema = new mongoose.Schema({
expert_id: String,
helpRequests: [
requestor_id: String,
topic_id: String
]
});
//query
const query = {
$and:[
{expert_id: req.body.expert_id},
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
]
};
// desired update
const update = {
$push: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
Expert.findOneAndUpdate(query, update, {new: true}, (err, expert) =>{
// handle return or error...
});
The reason you are not getting any expert is condition inside your query.
Results always returned based on the condition of your query if your condition inside query get satisfied you will get your result as simple as that.
Your query
{'helpRequests.requestor_id': {$ne: req.body.requestor_id}},
{'helpRequests.topic_id': {$ne: req.body.topic_id}}
you will get your expert only if requestor_id and topic_id is not exists inside helpRequests array. thats you are querying for.
Solution
As per you schema if helpRequests contains only requestor_id and topic_id then you can achieve what you desire by below query.
Expert.findOneAndUpdate(
{
expert_id: req.body.expert_id,
}, {
$addToSet: {
helpRequests: {
requestor_id: req.body.requestor_id,
topic_id: req.body.topic_id
}
}
}, { returnNewDocument: true });

Nodejs Mongoose Cannot update values in DB

I am pretty new to Nodejs and MongoDB. This might be a simple error, but I cannot seem to update values from DB. I searched far and wide but my syntax is seem to be right. Please help me.
var dataConstruct = {}
dataConstruct[fieldName] = fieldValue;
updateRecordModel.find(dataConstruct , function(err, field) {
if(err) {
logger.error("Error deleting records. Error -" + err)
callback(err, "Error while searching for record.Please cheack the values posted")
}
else {
logger.info("JSON"+JSON.stringify(field[0].Option1));
field[0].Option1="Max";
logger.info("JSON"+JSON.stringify(field[0].Option1));
if(field){
//code to update
}
else{
callback(err, "No such data present")
}
}
}).lean();
Find is returning data . I am using .lean() to get javascript object instead of document model object so that I can modify data. I cannot seem to modify the data returned without using .lean() as is the problem in this post Why can't you modify the data returned by a Mongoose Query (ex: findById)
I tried to find the data and update or resave it, I tried all the update syntax and methods(including find and then save doc again) mentioned in this post https://scotch.io/tutorials/using-mongoosejs-in-node-js-and-mongodb-applications but none seem to work,as I cannot modify the data returned
This is the json returned from find
{"_id":"564c29d96bf38ba3039f4844","Option1":"Gary","Option2":"Fred","__v":0}
I need to know how to update this value
This is the code I used for findoneand update
updateRecordModel.findOneAndUpdate({"Option1": "Gary" }, { "Option1": "Max" }, function(err,records) {
if (err) throw err;
console.log(records);
});
In console record is being returned without being updated
You can try after field[0].Option1="Max"; use save method. field[0].save() . I hope it help
Since mongoose 4.0 the returned value by the findOneAndUpdate is always the old document. If you want the new one to be returned (the updated document) you have to pass {new: true} in the options, as stated in their documentation.
You should end up with something like this:
updateRecordModel.findOneAndUpdate({"Option1": "Gary" }, { "Option1": "Max" }, {new: true}, function(err,records) {
if (err) throw err;
console.log(records);
});

Elasticsearch pagination with MongoDB and ExpressJS

I tried to search online for a solution but to be honest I still didn't found anything that help me to achieve this.
It is the first time I use ElasticSearch and I'm also pretty new with Node and MongoDB.
So, I followed a kind of tutorial and implemented Mongooastic from NPM to let ElasticSearch work with Node.
It seems to work fine even if on a total of 12 users indexed, if I type in the search "user" in the search list view I can find 12 records, it show 10 in a for each and the first one has missing values...
But the main problem for me is the pagination... or a sort of... it will be also nice to implement infinite scroll on it but I don't really know how to handle it.
So, the controller that handle it is the following at the moment:
exports.searchUsers = function(req, res) {
User.search({
query_string: {
query: req.query.q
}
},
{ hydrate: true },
function(err, results) {
if (err) res.send(err);
res.render('search-results', {
results: results,
users: results.hits.hits
});
});
};
I don't really know where to put size and from in here... and after understanding this I also would like to know how to implement, if possible, a sort of infinite scroll... And also how to handle link for the pagination... i.e.: prev, 1, 2, 3, 4, ..., next
The view has a simple input text for the search, after pressing submit it open a new page with the list of hits, so it should be nothing complex...
I hope you may help. Thanks
By default elasticsearch returns the 10 first results: you will have to set the size parameter if you want more.
Also, in order to add the size and from parameters, you need to write your query like so:
User.search({
query: {
query_string: {
query: req.query.q
}
},
size: 30,
from: 30
},
{hydrate: true},
function (err, results) {
if (err) res.send(err);
res.render('search-results', {
results: results,
users: results.hits.hits
});
});
(see here: https://github.com/taterbase/mongoosastic/issues/123 )
This will give you user n°30 up to user n°60 (more info here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html). You will have to play with your frontend to get the values you want for your size and from parameters.

Mongoose: multiple query populate in a single call

In Mongoose, I can use a query populate to populate additional fields after a query. I can also populate multiple paths, such as
Person.find({})
.populate('books movie', 'title pages director')
.exec()
However, this would generate a lookup on book gathering the fields for title, pages and director - and also a lookup on movie gathering the fields for title, pages and director as well. What I want is to get title and pages from books only, and director from movie. I could do something like this:
Person.find({})
.populate('books', 'title pages')
.populate('movie', 'director')
.exec()
which gives me the expected result and queries.
But is there any way to have the behavior of the second snippet using a similar "single line" syntax like the first snippet? The reason for that, is that I want to programmatically determine the arguments for the populate function and feed it in. I cannot do that for multiple populate calls.
After looking into the sourcecode of mongoose, I solved this with:
var populateQuery = [{path:'books', select:'title pages'}, {path:'movie', select:'director'}];
Person.find({})
.populate(populateQuery)
.execPopulate()
you can also do something like below:
{path:'user',select:['key1','key2']}
You achieve that by simply passing object or array of objects to populate() method.
const query = [
{
path:'books',
select:'title pages'
},
{
path:'movie',
select:'director'
}
];
const result = await Person.find().populate(query).lean();
Consider that lean() method is optional, it just returns raw json rather than mongoose object and makes code execution a little bit faster! Don't forget to make your function (callback) async!
This is how it's done based on the Mongoose JS documentation http://mongoosejs.com/docs/populate.html
Let's say you have a BookCollection schema which contains users and books
In order to perform a query and get all the BookCollections with its related users and books you would do this
models.BookCollection
.find({})
.populate('user')
.populate('books')
.lean()
.exec(function (err, bookcollection) {
if (err) return console.error(err);
try {
mongoose.connection.close();
res.render('viewbookcollection', { content: bookcollection});
} catch (e) {
console.log("errror getting bookcollection"+e);
}
//Your Schema must include path
let createdData =Person.create(dataYouWant)
await createdData.populate([{path:'books', select:'title pages'},{path:'movie', select:'director'}])

Categories

Resources