I have a collection of blogs that contains the title, body and agrregate ratings that users gave them. Another collection is โRatingsโ, whose scheme has a link to a blog, a user who rated (if at all evaluates them) in the form of their ObjectIds and the rating they gave, i.e. +1 or -1.
When a particular user views blogs in the "last first" order (say 40 of them per page. Call them an array from blogs[0]
to blogs[39]
). I need to get rating documents related to this particular user and those 40 blogs, if at all the user has rated them and informed him what ratings he gave to these blogs.
I tried to extract all the rating documents of a user in which the link objects of the reference object are between blogs[0]._id
and blogs[39]._id
, which returns an empty list in my case. It may not be possible to map object objects using the $lt
and $gt
queries. In that case, how do I do this? Should I redesign my circuits according to this scenario?
I am using the mongoosejs driver for this case. Here are the relevant parts of the code that are slightly different from the execution, but you get this idea.
Schemas:
Client= new mongoose.Schema({ ip:String }) Rates = new mongoose.Schema({ client:ObjectId, newsid:ObjectId, rate:Number }) News = new mongoose.Schema({ title: String, body: String, likes:{type:Number,default:0}, dislikes:{type:Number,default:0}, created:Date,
models:
var newsm=mongoose.model('News', News); var clientm=mongoose.model('Client', Client); var ratesm=mongoose.model('Rates', Rates);
Logics:
newsm.find({tag:tag[req.params.tag_id]},[],{ sort:{created:-1},limit: buffer+1 },function(err,news){ ratesm.find({client:client._id,newsid:{$lte:news[0]._id,$gte:news.slice(-1)[0]._id}},function(err,ratings){ }) })
Edit: Following the diagram below, I had to execute this request in the mongoose.js file
> db.blogposts.findOne() { title : "My First Post", author: "Jane", comments : [{ by: "Abe", text: "First" }, { by : "Ada", text : "Good post" } ] } > db.blogposts.find( { "comments.by" : "Ada" } )
How to make this request in mongoose?