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

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.

Related

iterate over large mongodb collection for purpose of updating schema

I have a 300k collection of test docs. I want to update all persons firstName and lastName to be lowercase.
const person = new Schema({
firstName: { type: String},
lastName: { type: String }
})
I've added lowecase:true to the schema but how do I update the existing documents?
I tried:
CaseFile
.find({ })
.cursor()
.eachAsync(async function (doc) {
await doc.save()
})
but i get the error
Error: Collection method find is synchronous
I also tried :
CaseFile
.find({ })
.then(docs => {
docs.forEach(doc => {
doc.save()
})
})
which gives the error:
JavaScript heap out of memory
db version v5.0.2
"mongoose": "^6.0.5",
thank you Wernfried Domscheit for the pipeline 🏄 solution:
CaseFile.updateMany({}, [
{
$set:
{
firstName: { $toLower: '$firstName' },
lastName: { $toLower: '$lastName' }
}
}]
)
.then(res => res)
Why on earth "iterate", i.e. line by line?
Use an aggregation pipeline:
db.CaseFile.updateMany({}, [
{ $set:
firstName: { $toLower: "$firstName" },
lastName: { $toLower: "$lastName" }
}
])

Mongoose findOneAndUpdate with $addToSet pushes duplicate

I have a schema such as
listSchema = new Schema({
...,
arts: [
{
...,
art: { type: Schema.Types.ObjectId, ref: 'Art', required: true },
note: Number
}
]
})
My goal is to find this document, push an object but without duplicate
The object look like
var art = { art: req.body.art, note: req.body.note }
The code I tried to use is
List.findOneAndUpdate({ _id: listId, user: req.myUser._id },
{ $addToSet: { arts: art} },
(err, list) => {
if (err) {
console.error(err);
return res.status(400).send()
} else {
if (list) {
console.log(list)
return res.status(200).json(list)
} else {
return res.status(404).send()
}
}
})
And yet there are multiple entries with the same Art id in my Arts array.
Also, the documentation isn't clear at all on which method to use to update something. Is this the correct way ? Or should I retrieve and then modify my object and .save() it ?
Found a recent link that came from this
List.findOneAndUpdate({ _id: listId, user: req.user._id, 'arts.art': artId }, { $set: { 'arts.$[elem]': artEntry } }, { arrayFilters: [{ 'elem.art': mongoose.Types.ObjectId(artId) }] })
artworkEntry being my modifications/push.
But the more I'm using Mongoose, the more it feels they want you to use .save() and modify the entries yourself using direct modification.
This might cause some concurrency but they introduced recently a, option to use on the schema { optimisticConcurrency: true } which might solve this problem.

How to project updated values only using findOneAndUpdate in embedded array Mongoose?

Currently my User model looks like:
{
_id: 'SomeId'
firstName: 'John',
lastName: 'Cena',
books: [
{
_id: 'xyz',
title: 'a',
author:'b',
ratings:[
{source:'source1', value:"8"},
{source:'source2', value:"9"}]
},
{
_id: 'abc',
title: 'c',
author:'d',
ratings:[
{source:'source3', value:"7"},
{source:'source4', value:"5"}]
}
]
}
After making an findOneAndUpdate query to update rating=>value of 1st book object(_id: "xyz") from 8 to 10 for a given source(say "source1"):
let up={
'books.$[book].ratings.$[rating].value':10
}
let filter={
new:true,
'books.rating':1, //I just want rating array of updated objects in it
arrayFilters:[
{ 'book._id':'xyz'},
{ 'rating.source': 'source1'}
]
}
User.findOneAndUpdate({'_id':'userId','books._id':'xyz'},up,filter).select('books.rating').exec((err,doc)=> {
if (err) throw err;
console.log(doc);
}
My code updates the books=>rating=>value correctly but I can't get that updated rating of that book.
This gives me rating of all books with both updated and non updated values in it. Looks like:-
{
books: [{ ratings:[{source:'source1', value:"10"},{source:'source2', value:"9"}] },
{ ratings:[{source:'source3', value:"7"},{source:'source4', value:"5"}] }]
}
I think the data of 2nd book shouldn't be there at all according to my code. I expect the follwing output:
{
books: [{ ratings:[{source:'source1', value:"10"}] }
}
Please help me to write findOneAndUpdate query correctly!
you can use array.find() like this:
const updatebookSource = (sourceId, userId, bookId) => {
User.findOneAndUpdate({ _id: userId, "books._id": bookId }, up, filter).exec(
(err, doc) => {
if (err) throw err;
let res = doc.books[0].ratings.find(rating => {
return rating.source === sourceId;
});
console.log(JSON.stringify(res, null, 1));
}
);
};
This returns the updated object. Let me know if it works.

Mongoose pull ObjectId from array

i'm trying to do a pretty simple operation, pull an item from an array with Mongoose on a Mongo database like so:
User.update({ _id: fromUserId }, { $pull: { linkedUsers: [idToDelete] } });
fromUserId & idToDelete are both Objects Ids.
The schema for Users goes like this:
var UserSchema = new Schema({
groups: [],
linkedUsers: [],
name: { type: String, required: true, index: { unique: true } }
});
linkedUsers is an array that only receives Ids of other users.
I've tried this as well:
User.findOne({ _id: fromUserId }, function(err, user) {
user.linkedUsers.pull(idToDelete);
user.save();
});
But with no luck.
The second option seem to almost work when i console the lenghts of the array at different positions but after calling save and checking, the length is still at 36:
User.findOne({ _id: fromUserId }, function(err, user) {
console.log(user.linkedUsers.length); // returns 36
user.linkedUsers.pull(idToDelete);
console.log(user.linkedUsers.length); // returns 35
user.save();
});
So it looks like i'm close but still, no luck. Both Ids are sent via the frontend side of the app.
I'm running those versions:
"mongodb": "^2.2.29",
"mongoose": "^5.0.7",
Thanks in advance.
You need to explicitly define the types in your schema definition i.e.
groups: [{ type: Schema.Types.ObjectId, ref: 'Group' }],
linkedUsers: [{ type: Schema.Types.ObjectId, ref: 'User' }]
and then use either
User.findOneAndUpdate(
{ _id: fromUserId },
{ $pullAll: { linkedUsers: [idToDelete] } },
{ new: true },
function(err, data) {}
);
or
User.findByIdAndUpdate(fromUserId,
{ $pullAll: { linkedUsers: [idToDelete] } },
{ new: true },
function(err, data) {}
);
I had a similar issue. I wanted to delete an object from an array, using the default _id from mongo, but my query was wrong:
const update = { $pull: { cities: cityId }};
It should be:
const update = { $pull: { cities: {_id: cityId} }};

Finding ID using MongoDB .find()

I am trying to loop through something but first I need to do a mongo query to get the data for the loop my query is this:
AP.find({}, function(err, allAP) {
var ID = req.user.id;
if(err){
console.log(err);
} else {
res.locals.aps= allAP; // Set the data in locals
next();
}
});
I know I need to add something in the {} part on line 1. How do I take req.user.id and then only find documents with the author.id (see below)
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
email: String
}
Example document currently returned:
{ _id: 59e517230a892a26cb1b7635,
manufacturer: 'other',
model: 'Test Model',
bands: '2.4',
channel: 11,
notes: 'I am a test note for the first access point',
__v: 0,
author: { id: 59d7f98a77fcc221d6e3c93d, email: 'joe#example.com' } },
You can do
AP.find({"author.id":req.user.id}, function(err, allAP) {
if(err){
console.log(err);
} else {
res.locals.aps= allAP; // Set the data in locals
next();
}
});