Query MongoDB to display only one section of data - javascript

I have created a MongoDB database and have it hooked up to a MEAN stack web application I have created. I have it able to display the most recent set of data put into it rather than all of the data but now I want to take that down to display only 1 section of that data, as there are currently 50 different sections.
I am using post.find and then sorting the data to show only the most recent record from the DB but I am struggling to break it down to show only one part of that data. Current data: https://imgur.com/a/cwKTCEa.
As you can see the data is grouped by "S0" and then data follows, then there is "S1" etc... In essence what I want is for just "S0" to be displayed when queried.
exports.list = function (req, res) {
Post.find().sort({ _id: -1 }).limit(1)
.then(function (posts) {
return res.status(200).json({
status: 200,
data: posts,
message: 'Success'
})
})
.catch(function (err) {
return res.status(400).json({
status: 400,
message: err.message
});
});
}
I believe I need to add to the find query but I am not sure how to specify that I would only like to see "S0" rather than S0-S49.
Thank you

If you want to return only specific fields from query you should use Project Fields:
Post.find({}, {"s0": 1}).sort({ _id: -1 }).limit(1)
Here is the docs Project Fields

Related

MongoDB update many using the values of the user

I've written a function to execute hourly which looks up a user and finds some values and then pushes those values into a history collection that records the hourly updated values. I've written this so far as a test just finding a user by their ID but now I need to roll this out to my entire database of 50,000+ users.
From what I've read using updateMany is a lot more performant but I'm not entirely sure how to retrieve the document detail of the record that is being updated at the time.
Here is my code so far, which you can see I'm first looking up the user and then grabbing their valuation details which I'd like to then push into a history collection.
exports.updateUserValuationHistoric = () => {
User.find({ _id: "609961fdd989613914ef7216" })
.populate('UserValuationHistory')
.exec((err, userDoc) => {
if (err){
console.log('[ERROR]: ', err)
}
const updatedValuationHistory = {
totalValuation: userDoc[0].valuation.totalValuation,
comicsValuation: userDoc[0].valuation.comicsValuation,
collectiblesValuation: userDoc[0].valuation.collectiblesValuation,
omiValuation: userDoc[0].valuation.omiValuation
}
UserValuationHistory.findOneAndUpdate(
{ user: userDoc[0]._id },
{ $push: {
'history': updatedValuationHistory
}},
{upsert: true, new: true}
)
.exec((error, updated) => {
if (error){
console.log('[ERROR]: Unable to update the user valuation history.')
} else {
console.log('[SUCCESS]: User historic valuation has been updated.')
}
})
})
}
Any help is greatly appreciated!
User model:
https://pastebin.com/7MWBVHf3
Historic model:
https://pastebin.com/nkTGztJY

Pagination with populated data in mongo

I'm struggling with a problem of fetching data from DB and applying "pagination" functionality. My case is that I have webstore with multiple users. Every user can publish their profile and create their products. On the website, I'd like to show the products of users who did publish their profiles.
Currently, this is my code:
Product.find({ active: true, deleted: false })
.sort({ created: -1 })
.populate({
path: 'user',
match: { isPublished: true },
select: 'details slug name',
})
.exec((error, products) => {
if (error) {
return response.status(500).send({ error: 'There was an error.' });
}
if (products === null) {
return response.status(404).send({ error: 'Products not found.' });
}
const productsWithPublishedTrainerProfile = _.filter(products, product => !_.isNull(product.user));
return response.status(200).send(_.map(productsWithPublishedTrainerProfile, product => mapProduct(product)));
});
It works - It fetches all the active products, populate the user who created the product and if their profile is published then the product is not filtered out.
Now I need to implement "pagination" functionality which means that I want to use .skip() and .limit() functions but this skip/limit will be run on all the products (not only the filtered ones.) what means that the endpoint won't be returning the exact number of products (set in the limit() function.
I wonder if there is any solution for that. I read about .aggregate with setting $lookup, $skip and $limit but after hours of fighting with it I couldn't make it work.
I will really appreciate any help ✌️

CastError: Cast to date failed for value date at path

I'm trying to make a query in my database developed in Mongo, we have the schema Examen (or Exam) and we need to find all the exams between two dates.
My system can get both dates and make the Query with this form (im using Moment as well in order to get the correct format)
This is the query that I get from the website
{ "examDate": { "$gt": { "$date": "1556686800000" }, "$lt": { "$date": "1559192400000"} } }
when I run this query in Studio3T it works normally but in the website it has an error:
CastError: Cast to date failed for value "{ '$date': '1559192400000'
}" at path "examDate" for model "Examen"
I have tried to change the '$date' format using the ISO format as well, that didn't work. And is weird that the query works normally in Studio3T but it dont works like this using the Mongo db.find() function.
//this is how I make the find function for my schema
Examen.find((query), (err, info_examen) => {
if (err) return res.status(500).send({ message: `Error: ${err}` })
if (!info_examen) return res.status(404).send({ message: `there is no data` })
res.status(200).send({ info_examen })
})
I expect to get the result (info_examen) in order to make statistics with the data.

Dynamic html form generation in MEAN stack

I've just started learning on MEAN stack and need to generate dynamic forms on the fly.
The requirement is to import document (excel/csv/xml/xls etc..) and generate dynamic forms using it so a user can update their data and again export it into the respective file.
So to accomplish this I'm converting documents to JSON format and storing JSON data into the MongoDB database.
Ex: Consider this xlsx data:
ID Name dob Gender
1 user1 7-Dec-87 m
2 user2 8-Dec-87 f
3 user3 9-Dec-87 f
3 user4 4-Dec-87 m
And I'm converting this using xlsx-to-json module to JSON format and storing it into Mongodb.
app.post('/myapp', function (req, res) {
//console.log("===========" + req.file.path);
converter({
input: req.file.path,
output: "output.json"
}, function (err, result) {
if (err) {
console.error(err);
} else {
console.log(result);
db.collection('test').insert(result, function (err, doc) {
console.log(err);
res.json(doc);
});
}
});
});
Here I'm fetching above data from Mongodb & express.js
app.get('/myapp', function (req, res) {
db.collection('test').find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.get('/birthdaylist/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.collection('test').findOne({_id: mongojs.ObjectId(id)}, function (err, doc) {
console.log(JSON.stringify(doc));
res.json(doc);
});
});
and here's the JSON output:
[
{ dob: '7-Dec-87', ID: '1', Name: 'user1' },
{ dob: '8-Dec-87', ID: '2', Name: 'user2' },
{ dob: '9-Dec-87', ID: '3', Name: 'user3' },
{ dob: '4-Dec-87', ID: '4', Name: 'user4' }
]
So, I've few queries:
Is this the correct approach I'm doing to generate dynamic form from xlsx/csv..etc ? If yes, then how can I generate dynamic form from above JSON.
While exploring on google I've found mongodb generates form automatically (https://github.com/GothAck/forms-mongoose)
So will it help because there may be chance of huge data on excel files.
Any help would be really appreciated.
Do you actually need to analyze an arbitrary spreadsheet and dynamically extract the schema, or do you know the schema ahead of time? If you know the schema, then the Mongoose form generating example is straightforward. But make sure that is actually a requirement because it is tough.
You are never going to be 100% because spreadsheets are created by users and users do weird things. But you can make something that works most of the time.
You need something that takes a JSON object and extracts the schema and puts that in a Mongoose schema format.
So you want to add an interesting module to Mongoose schema. I searched node-modules.com and this came up: https://github.com/Nijikokun/generate-schema
Form generation is not a trivial task. You may want to consider using a library for this. Here are a few that might be useful to you:
http://schemaform.io/
https://github.com/jdorn/json-editor/
Also, if you need help generating JSON schema from JSON:
http://jsonschema.net/#/
and of course: http://json-schema.org/

mongoose update array ObjectId from an array from angular

I'm adding ObjectId to an array from another array that I receive as the body.
exports.updateBasket = function (req, res) {
Basket.findOne({ _id: req.params.id }, function (err, basket) {
for(var i=0, len=req.body.length; i < len; i++) {
basket.update({$addToSet: { "items": req.body[i] } }, { upsert: true, safe: true });
}
if (err) {
res.send(err);
}
else {
res.json({ message: 'Successfully added' });
}
});
};
I have 2 questions concerning this :
Is there any upside to do the loop in angular and have multiple PUT?
What is the way to update this same array but when removing ObjectId?
One way that I thought of was to loop ObjectId that have to be removed and look if they are in the array of the object, if yes, delete them.
Another way would be to clear the array when PUT is called and update with the new ObjectId list (which would be the ones that were there minus the one user removed).
Both doesn't feel right ...
thanks
You code looks a bit odd. You are fetching asynchronously on the req.params._id but you are queuing up req.body.length potential worth of updates, but you send 'success' before you even get a response back from the updated results.
If you wanted to filter on arrays, look at lodash, if you want to process multiple updates asynchronously and get those response use async modules.

Categories