mongoose populate and sort nested item - mongodb

I have a simple model:
user = {
'items': [{
'name': 'abc',
'pages': [ObjectId("58c703a353dbaf37586b885c"), ObjectId("58c703a353dbaf37586b885d"), ..]}
}]
};
I'm trying to sort the pages of current item:
User.findOne({'_id': id}, {'items': {$elemMatch: {'_id': id2}}})
.populate({path: 'items.pages', select: '_id', options: { sort: { _id: -1 } }})
.exec(function(err, user) {
});
But I'm getting an error: Error: Cannot populate withsorton path items.pages because it is a subproperty of a document array. What should I change?

Related

How to iterate on mongoose subdocument array of objects

Trying to implement conditional statement relying on subdocument array of objects, so i need to iterate over collection of users in database and check inside each user subdocument array of objects with findIndex as for javascript
Users collection
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
required: true,
lowercase: true
}
friends: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
family: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
acquaintances: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
following: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User"
}
],
pendingFriendConfirmationData:[
{
storedUserId : {type: String},
choosenCategories: [{label: {type: String}, value: {type: String}}]
}
]
});
const Users = mongoose.model("Users", userSchema);
module.exports = Users;
now i can access Users collection with
db.Users.find()
my example result for
let filter = {"_id": userId}
let projection = {username: 1, friends: 1, family: 1, acquaintances: 1, following: 1, pendingFriendConfirmationData: 1}
db.Users.findOne(filter, projection, (err, user)=>{
console.log(user)
})
{
friends: [],
family: [],
acquaintances: [],
following: [],
_id: 5ca1a43ac5298f8139b1528c,
username: 'ahmedyounes',
pendingFriendConfirmationData: [
{
choosenCategories: [Array],
_id: 5ccb0fcf81a7944faf819883,
storedUserId: '5cc95d674384e302c9b446e8'
}
]
}
focusing on pendingFriendConfirmationData
the following screenshot from MongoDB Compass
I want to iterate over like this
let filter = {"_id": userId}
let projection = {username: 1, friends: 1, family: 1, acquaintances: 1, following: 1, pendingFriendConfirmationData: 1}
db.Users.findOne(filter, projection, (err, user)=>{
let data = user.pendingFriendConfirmationData
for(let i in data){
if(data[i].choosenCategories.findIndex(v => v.label === "friend") !== -1){
console.log("he is a friend")
}
}
})
How to iterate over pendingFriendConfirmationData and choosenCategories
like above
for now if i console.log(data) as following
db.Users.findOne(filter, projection, (err, user)=>{
let data = user.pendingFriendConfirmationData
console.log(data)
})
I get
I figured it out Faster Mongoose Queries With Lean
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents are plain JavaScript objects (POJOs), not Mongoose documents. In this tutorial, you'll learn more about the tradeoffs of using lean().
In my previous example the solution would be adding {lean: true}
db.Users.findOne(filter, projection, {lean: true}, (err, user)=>{
let data = user.pendingFriendConfirmationData
console.log(data)
})
also here
db.Users.findOne(filter, projection, {lean: true}, (err, user)=>{
let data = user.pendingFriendConfirmationData
for(let i in data){
if(data[i].choosenCategories.findIndex(v => v.value === "friends") !== -1){
console.log("he is a friend")
}
}
})
// he is a friend
Conclusion
to iterate over deeply nested subdocument array of objects you need to make sure
that you are working with plain JavaScript objects (POJOs) using lean()
db.Users.find().lean()

getting count within a findOne of a mongoose schema array field

I want to use Mongoose to return information about a user to populate their profile. I've been using findOne to populate a list of their comments along with basic profile information through embedded documents and with .populate. I want to get a count of the friends that they have by counting how many objects are in the friends array.
It looks like aggregate is one of doing that, but how can I use both? or is there a simple way of doing a count in the findOne query?
var UserSchema = new Schema({
username: String,
comments : [{ type: Schema.Types.ObjectId, ref: 'Comment' }],
friends: [
{
id: { type: Schema.Types.ObjectId, ref: 'User' },
permission: Number
}
]
})
var User = mongoose.model('User', UserSchema);
var Comment = mongoose.model('Comment', CommentSchema);
app.get('/profile/:username', function(req, res) {
User
.findOne({ username: req.params.username }, 'username friends -_id')
.populate({
path: 'current',
model: 'Comment',
select: 'comment author -_id date',
populate: {
path: 'author',
model: 'User',
select: 'username firstName lastName -_id'
}
})
.exec(function(err, user) {
//
})
)}
If user returns with friends array, why don't you return just user.friends.length ?
If you want just count, use this
User.aggregate([
{
$match: { username: req.params.username }
},
{
$unwind: "$comments"
},
{
$lookup: {
from: "Comment",
localField: "comments",
foreignField: "_id",
as: "comments"
}
},
{
"$group": {
"_id": "$_id",
"friends": { "$first": "$friends"},
"comments": { "$push": "$comments" }
}
},
{
$project: {
_id: 0,
count: {$size: '$friends'},
comments: 1,
username: 1
}
}
]).exec() ...

Waterline: How to perform IN queries if attribute is a collection?

In the docs of waterline it is stated that this is the way to perform a IN query on a model:
Model.find({
name : ['Walter', 'Skyler']
});
And this the way to perform an OR query on a model:
Model.find({
or : [
{ name: 'walter' },
{ occupation: 'teacher' }
]
})
My problem now is that i need a combination of those two, and to make it even more complicated, one of the attributes i have to use is a collection.
So what i tried is this, but it doesn't seem to work:
Product.find({
or : [
{ createdBy: userIds },
{ likes: userIds }
]
})
Note: userIds is an array of id's from a user model.
The (simplified) product model looks likes this:
module.exports = {
attributes: {
name: 'string',
description: 'string',
createdBy: {
model: 'User'
},
brand: {
model: 'Brand',
},
likes: {
collection: 'User',
}
}
}
The query works when I only include createdBy, so it seems to be a problem with the collection attribute.
Is this somehow possible?
Thank you for your input.
UPDATE:
I think this is only possible with native() queries.
The way I understand it something like this should work.
Product.native(function(err, products){
if(err) return res.serverError(err);
products.find({"likes": { $elemMatch: { _id: { $in: userIds}}}}).toArray(function(err, results){
if (err){
console.log('ERROR', err);
}
else {
console.log("found products: " + results.length);
console.log(results);
return res.ok(results);
}
});
});
Unfortunately, it doesn't. The returned results is always an empty array.

MongoDB: multiple $elemMatch

I have MongoDB documents structured like this:
{_id: ObjectId("53d760721423030c7e14266f"),
fruit: 'apple',
vitamins: [
{
_id: 1,
name: 'B7',
usefulness: 'useful',
state: 'free',
}
{
_id: 2,
name: 'A1',
usefulness: 'useful',
state: 'free',
}
{
_id: 3,
name: 'A1',
usefulness: 'useful',
state: 'non_free',
}
]
}
{_id: ObjectId("53d760721423030c7e142670"),
fruit: 'grape',
vitamins: [
{
_id: 4,
name: 'B6',
usefulness: 'useful',
state: 'free',
}
{
_id: 5,
name: 'A1',
usefulness: 'useful',
state: 'non_free',
}
{
_id: 6,
name: 'Q5',
usefulness: 'non_useful',
state: 'non_free',
}
]
}
I want to query and get all the fruits which have both {name: 'A1', state: 'non_free'} and {name: 'B7', state: 'free'}.
In the worst case I want at least to count these entries if getting them is not possible and if the equivalent code exists for pymongo, to know how to write it.
For the given example I want to retrieve only the apple (first) document.
If I use $elemMatch it works only for one condition, but not for both. E.g. if I query find({'vitamins': {'$elemMatch': {'name': 'A1', 'state': 'non_free'}, '$elemMatch': {'name': 'B7', 'state': 'free'}}}) it will retrieve all the fruits with {'name': 'B7', 'state': 'free'}.
In this case you can use the $and-operator .
Try this query:
find({
$and: [
{'vitamins': {'$elemMatch': {'name': 'A1', 'state': 'non_free'} } },
{'vitamins': {'$elemMatch': {'name': 'B7', 'state': 'free'} } }
]
});
To explain why you received only the result matching the second criteria: The objects inside each {} you pass to MongoDB are key/value pairs. Each key can only exist once per object. When you try to assign a value to the same key twice, the second assignment will override the first. In your case you assigned two different values to the key $elemMatch in the same object, so the first one was ignored. The query which actually arrived in MongoDB was just find({'vitamins': {'$elemMatch': {'name': 'B7', 'state': 'free'}}}).
Whenever you need to apply the same operator to the same key twice, you need to use $or or $and.
var fruits = db.fruits.find({
"vitamins": {
$all: [{
$elemMatch: {
"name": "A1",
"state": "non_free"
}
}, {
$elemMatch: {
"name": "B7",
"state": "free"
}
}]
}
})
let query = [];
query.push({
id: product.id,
});
query.push({ date });
for (const slot of slots) {
query.push({
slots: {
$elemMatch: {
id: slot.id,
spots: { $gte: slot.spots },
},
},
});
}
const cal = await Product.findOne({ $and: query });

MongoDB nested collection query of same collection

How do you do a nested query when you need the results of the same collection in the next query?
var mongo = require('../config/mongo');
var mongoDB = mongo.db;
...
exports.myFunction = function(req, res) {
...
...
// e.g. myArray = ['a','b','c'];
mongoDB.collection('MyCollection', function(err, collection) {
collection.find({ $or: [{ 'source': {$in: myArray} },{ 'target': {$in: myArray} }]}, { "someVar": 0}).toArray(function(err, firstResults) {
var allResults = [];
for (var i = 0; i < firstResults.length; i++) {
allResults[firstResults[i].source]=1;
allResults[firstResults[i].target]=1;
};
var secondResults = Object.keys(allResults);
mongoDB.collection('MyCollection', function(err, collection) {
collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
res.send(items);
});
});
});
});
But it doesn't like that I am calling the same collection 'MyCollection' twice. I'm trying to get any document whose source or target involves secondResults.
You should get rid of the second call to open 'MyCollection'. You already have a 'MyCollection' object after opening it once. Namely, you have this:
mongoDB.collection('MyCollection', function(err, collection) {
collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
res.send(items);
});
});
but you already have an instance of 'MyCollection' from here:
mongoDB.collection('MyCollection', function(err, collection) {
collection.find({ $or: [{ 'source': {$in: myArray} },{ 'target': {$in: myArray} }]}, { "someVar": 0}).toArray(function(err, firstResults) {
So since the second call to 'collection' is in the scope of the first call, you can (and should) reuse that instance:
collection.find({ $or: [{ 'source': {$in: secondResults} },{ 'target': {$in: secondResults} }]}, { "someVar": 0}).toArray(function(err, items) {
res.send(items);
});
Alternatively, if you're using node-mongodb-native, you can have a look at some of the examples to see other ways of accomplishing what you're after.
UPDATE 1
This code works for me:
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db){
if(err) throw err;
var collection = db.collection('c');
var secondQuery = {};
collection.find({a:'b'}).toArray(function(err, firstRes){
for(var i = 0; i < firstRes.length; i++){
secondQuery[firstRes[i].a] = firstRes[i].b;
}
collection.find(secondQuery).toArray(function(err, secondRes){
for(var i = 0; i < secondRes.length; i++){
console.log(secondRes[i]);
}
db.close();
});
});
});
UPDATE 2
Note that the above code is using node-mongodb-native version 1.3.19. Since version 1.2, it is recommended to use MongoClient when interacting with mongodb from Node.js driver.