deep find() query in Mongoose - node.js

so my schema looks like something like this
var PagesSchema = new mongoose.Schema({
citiesList:{
country:String,
city:String
}
});
i want to access citiesList in my route so i tried
app.get("/", function(req,res){
Pages.find({"citiesList"}, function(err,citiesList){
if(err){
console.log(err);
}else{
res.render('landing',{citiesList:citiesList});
}
});
});
but it's not working any advice please ?

That query is searching for any document that look like {citiesList: "citiesList"}. If you want all "citiesList", you can instead use {} to find all documents, and a projection to limit it to the citiesList field: Pages.find({}, "citiesList", cb)
https://mongoosejs.com/docs/api.html#model_Model.find
If instead you want only those subdocuments in a formatted list, you can use an aggregation to process them

Related

how to get the specific result according to a certain condition in MongoDb through mongoose

I want to get the articles of the specific user through mongoose, how can I do it? as my code:
Content.find().sort({_id:-1}).populate(['category','user']).then(function (content) {
res.render('admin/content_list',{
pid:req_pid,
title:'博客管理后台',
userInfo:req.userInfo,
contents: content
})
})
I get all the articles through this, but what i want is a result that belongs to certain user, how can I do it?
You can do:
Content.find({userid:USERID}).sort({_id:-1}).populate(['category','user']).then(function (content) {
res.render('admin/content_list',{
pid:req_pid,
title:'博客管理后台',
userInfo:req.userInfo,
contents: content
})
})
Where USERID is the ObjectId of the user.
You probably also want to take a look at this: http://mongoosejs.com/docs/queries.html, assuming you are using mongoose.
You can filter results for a specific user by using their objectId. As long as you have that ID you can do something like this:
const mongoose = require('mongoose');
const ObjectId = require('mongoose').Types.ObjectId;
Content.find({
user: new ObjectId(userId)
})
.sort({_id: -1})
.populate(['category', 'user'])
.exec((err, results) => {
});

Mongo / Express Query Nested _id from query string

Using: node/express/mongodb/mongoose
With the setup listed above, I have created my schema and model and can query as needed. What I'm wondering how to do though is, pass the express request.query object to Model.find() in mongoose to match and query the _id of a nested document. In this instance, the query may look something like:
http://domain.com/api/object._id=57902aeec07ffa2290f179fe
Where object is a nested object that exists elsewhere in the database. I can easily query other fields. _id is the only one giving an issue. It returns an empty array of matches.
Can this be done?
This is an example and not the ACTUAL schema but this gets the point across..
let Category = mongoose.Schema({
name: String
})
let Product = mongoose.Schema({
name: String,
description:String,
category:Category
})
// sample category..
{
_id:ObjectId("1234567890"),
name: 'Sample Category'
}
// sample product
{
_id:ObjectId("0987654321"),
name:'Sample Product',
description:'Sample Product Description',
category: {
_id:ObjectId("1234567890"),
name: 'Sample Category'
}
}
So, what I'm looking for is... if I have the following in express..
app.get('/products',function(req,res,next){
let query = req.query
ProductModel.find(query).exec(function(err,docs){
res.json(docs)
})
})
This would allow me to specify anything I want in the query parameters as a query. So I could..
http://domain.com/api/products?name=String
http://domain.com/api/products?description=String
http://domain.com/api/products?category.name=String
I can query by category.name like this, but I can't do:
http://domain.com/api/products?category._id=1234567890
This returns an empty array
Change your query to http://domain.com/api/object/57902aeec07ffa2290f179fe and try
app.get('/api/object/:_id', function(req, res) {
// req._id is Mongo Document Id
// change MyModel to your model name
MyModel.findOne( {'_id' : req._id }, function(err, doc){
// do smth with this document
console.log(doc);
});
});
or try this one
http://domain.com/api/object?id=57902aeec07ffa2290f179fe
app.get('/api/object', function(req, res) {
var id = req.param('id');
MyModel.findOne( {'_id' : id }, function(err, doc){
console.log(doc);
});
})
First of all increase your skills in getting URL and POST Parameters by this article.
Read official Express 4.x API Documentation
Never mind I feel ridiculous. It works just as I posted above.. after I fixed an error in my schema.

replace whole collection with new with same name using mongoose and nodejs

Here i am trying to save collection with name courseTitle in
database using monggose and nodejs and if any collection exists with
the same name(courseTitle) while saving then it should replace the existing
collection. How can I replace the existing collection with new collection whenever i save, without using remove() and save().
My approach: first i deleted(remove()) the collection if exists and then saved(save()) again with same name.
Is there any other technique of doing same?
var mongoose = require('mongoose');
var courseSchema = require('./video-course-events');
module.exports.saveEvent = function(req, res){
var courseTitle = req.body.CourseDetails.courseTitle;
var Vcevents = mongoose.model(courseTitle, courseSchema);
var vcevents = new Vcevents(req.body);
Vcevents.find(function (err, result){
if(result.length)
{
// replace the collection(eg, mycourse) with new using same name(ie, mycourse).
}
else if(err){
console.log('Error occured..!!', err);
}
else{
vcevents.save(function (err, results){
if(err){
res.json(err);
}
else{
res.json(results);
}
});
}
});
}
You could use mongoose findOneAndUpdate to achieve something similar without wiping out the collection. My mongoose is a little shaky but I think this would work.
Vcevents.findOneAndUpdate({}, vcevents.toObject(), {upsert: true}, function(err, doc){ //execute query });
Another potential option would be to use a capped collection. you could initialize the size of the collection one. This way every insert you make should get rid of the oldest document and replace it with the newest one. I've never personally used a capped collection but this is in theory how it should work according to the docs. I don't know if such a small limit would give it any problems.
db.createCollection("Vcevents", { capped : true, size : 5242880, max : 1 } )

How to loop over mongodb array with express and monk to create a page for each element?

I am building a web app with express, nodejs and monk. I am trying to create a page for each element of an array in my mongodb database.
That data is already in the collection called collections with the key coll_list like so:
{ "_id" : ObjectId("53dbaefd3d85d57492506f1f"), "coll_list" : [ "data_pagename1",
"data_pagename2", "data_pagename3" ] }
I thought it might be possible to loop over all the elements in coll_list with something like:
router.get('/index', function(req, res) {
var db = req.db;
var collection = db.get('collections');
collection.find( "coll_list" , function(e,docs) {
for (elems in docs) {
res.render(elems, {
elems : docs
});
}
});
});
Any suggestions or help/pointers on how to do this would be greatly appreciated.
Use req.params
router.get('/coll/:id',
function(req,res){
//access the id by req.params.id
//req.params.id will essentially be the _id of the document
//use it to obtain data from mongoDB and render the page using that data
//From the front end you make the call to /coll/<ObjectId> like
// /coll/53dbaefd3d85d57492506f1f and you get that id in req.params.id and use it to
//render data specific to that _id.
});
Thus, using a single route you would be able to create a page for every item in coll_list

Querying nested documents using Mongoose (MongoDB)

I am starting out with mongodb and having hard time trying to query nested documents. I have two schemas:
var LinkSchema = new mongoose.Schema({
url: String,
name: String
});
var UserSchema = new mongoose.Schema({
name: String,
links: [LinkSchema]
});
As you can see, I am just tying to build a simple bookmarking tool. Each user has a name and a collection of links. Each link has a name and a url.
Now, what I am trying to do is for example, see if a link already exists in someone's links array. I would like to be able to do something like this (Trying to get vlad's link collection and then see if the query link already belongs to the collection or not):
app.get("/:query", function(req, res){
User.findOne({"name":"vlad"}, function(err, user){
user.links.find({"url":req.params.query}, function(err, foundLinks){
if(foundLinks){
res.send("link already exists!");
} else {
res.send("link doesn't exist!");
}
});
});
});
Of course, this code doesn't work, because apparently I can't do a "user.links.find()". I guess I can just do a user.links.map to extract only urls and then run a membership query against it. But I think this would be far from the right solution. There's gotta be a way to do something like this natively using DB queries. Can someone help? Thank you!
You can query an embedded document in mongoose like this
User.find({'links.url':req.params.query}, function(err, foundUsers){
// ---
});
and to find the links that belong to the user "vlad", you can write
User.find({name:'vlad','links.url':req.params.query}, function(err, foundUsers){
// ---
});
This will do the trick.
To find a specific link that belongs to a specific user you can do this
User.find({name:'vlad','links.url':req.params.query}, { 'links.$': 1 }, function(err, foundUsers){
// ---
});

Resources