ExpressJS and MongooseJS: can I query array values in a subdocument? - node.js

I've got a web api written using expressjs and mongoosejs.
The main schema in the app contains a subdocument, permissions, which contains array fields. Those arrays contain ids of users who can perform an action on that document.
I'm trying to limit results of a query on that collection by the values in the read subdocument array field:
Here's the main schema:
var MainSchema = new Schema({
uri: { type: String, required: false },
user: { type: String, required: false },
groups: [String],
tags: [String],
permissions: {
read: [String],
update: [String],
delete: [String]
}
});
I want to return documents that have a specific value in the permissions.read array or where that array is empty.
My code doesn't throw an error, but doesn't limit the results; I still get documents that don't match the given value, and aren't empty.
Here's what I've got:
var MainModel = mongoose.model('MainSchema', MainSchema);
app.get('/api/search', function (req, res) {
var query = MainModel.find({'uri': req.query.uri });
// This works fine
if (req.query.groups) {
query.where('groups').in(req.query.groups);
}
else if (req.query.user) {
query.where('user').equals(req.query.user);
}
// How can I return only documents that match req.query.user or ""?
query.where('permissions.read').in([req.query.user, ""]);
query.exec(function (err, results) {
if (!err) {
return res.send(results);
} else {
return console.log(err);
}
});
});
I have a hunch that the where clause is not testing the value of each of the elements of permissions.read against each of the values of the array passed to the in clause.
Thanks.
EDIT: Here's a document that shouldn't be returned, but is (note, permissions.read array includes a value that's not the current user's ID):
{
"user": "firstname#gmail.com",
"uri": "http://localhost:3000/documents/test",
"permissions": {
"delete": [
"firstname#gmail.com"
],
"update": [
"firstname#gmail.com"
],
"read": [
"firstname#gmail.com"
]
},
"tags": [],
"groups": [
"demo",
"2013"
]
}
EDITED: Corrected Model/Schema confusion, which wasn't in my code, but was left out in the copy/paste. Thanks.

I have looked for and not found an example of a mongodb/mongoose query that tests either for a specified value in a subdocument array field or an empty array field.
Instead, since I control both the API (which this code comes from) and the client application, I refactored.
I now add one of three where clauses to the query, based on client-side user selection:
if (req.query.mode === 'user') {
query.where('user').equals(req.query.user);
}
else if (req.query.mode === 'group') {
query.where('groups').in(req.query.groups);
query.$where('this.permissions.read.length === 0');
}
else if (req.query.mode === 'class') {
query.$where('this.permissions.read.length === 0');
}
Thanks for your input, #JohnnyHK.

Related

Updating an Array at a specific Index using Mongoose

I have a collection of documents in mongoDB. One of the schema's properties has an array, that contains objects. Each object within this array contains a property that has another array as it's value.
const userSchema = new mongoose.Schema({
username: "",
password: "",
firstName: "",
clients: [],
});
The "clients" property array looks like this:
[
{
clientName: 'Chabad of Closter',
activeInvoice: [],
pastInvoices: []
},
{
clientName: 'Chabad UC',
activeInvoice: [],
pastInvoices: []
},
{
clientName: 'Chabad Mobile',
activeInvoice: [],
pastInvoices: []
}
]
My goal is to push an Object into any of the "activeInvoice" arrays by using the index of its object. I used this code and it works when I specify the index manually:
User.findByIdAndUpdate(id, {"$push": {"clients.2.activeInvoice": newCharge}}, {new : true},
function(err, updatedCharge){
if (err) {
console.log(err)
} else {
console.log(updatedCharge);
}
});
In the example above, I used the "2" index. I need to be able to change that index dynamically. I tried this:
// code to find the index I want and save it to indexer
const indexer = clientsArr.findIndex(i => i.clientName == newCharge.clientName);
// form it into a string
const mongooseLink = "clients." + indexer + ".activeInvoice";
//place it into the mongoose request
User.findByIdAndUpdate(id, {"$push": {mongooseLink: newCharge}}, {new : true},
function(err, updatedCharge){
if (err) {
console.log(err)
} else {
console.log(updatedCharge);
}
});
but this doesn't work. I double checked to make sure the indexer is working. No error, just the document doesn't get updated.

mongoose duplicate items getting inserted using $addToSet and $each to push items into the array

I am trying to push an array of objects into a document. I am using $addToSet to try and not insert duplicate data. I want to do a check on applied.studentId. But if I pass the same request twice, then the data is getting inserted. Is there any check on $addToSet and $each that I have to use?
My schema is as follows
jobId: { type: Number},
hiringCompanyId: String,
applied: [{
studentId: String,
firstName:String,
lastName:String,
gender:String,
identityType:String,
identityValue:String,
email:String,
phone:String,
}],
My node code is as follows.
public ApplyForJob(data: JobDto): Promise<{ status: string }> {
let students = data.applied;
let findQuery = {hiringCompanyId: data.hiringCompanyId, jobId: data.companyJobId};
let appliedQuery = {};
if (!isNullOrUndefined(data.applied.length)) {
appliedQuery = {
"$addToSet": {
"applied": {
"$each": data.applied
}
}
};
}
return new Promise((resolve, reject) => {
Jobs.findOneAndUpdate(findQuery, appliedQuery).exec((err, info) => {
if (err) {
reject(new UpdateError('Jobs - Update()', err, Jobs.collection.collectionName));
} else {
console.log(info);
resolve({status: "Success"});
}
})
});
}
On disabling the date field, $addToSet does not add duplicate values. As per the doc https://docs.mongodb.com/manual/reference/operator/update/addToSet/
As such, field order matters and you cannot specify that MongoDB compare only a subset of the fields in the document to determine whether the document is a duplicate of an existing array element.
as Rahul Ganguly mention absolutely correctly, we cannot use reliably $addToSet with JS objects.
One options is to move applied in to separate collection and make Job schema to ref new Applied model.
Example:
{
jobId: { type: Number },
hiringCompanyId: String,
applied: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Applied'
}]
}

Trying to find record using $in in mongoose

I'm trying to find record using $in in mongoose. But it's not working for me. I have same query in mongo shell its working but in mongoose it is not workinh
my schema
{
"_id": "574f1f979f44e7531786c80f",
"name": "mySchool2",
"branch": "karachi",
"location": "Clifton ",
"block": false,
"created_at": 1464803223441,
"updated_at": 1464803223441,
"__v": 0,
"classes": [
"574f216afd487958cd69772a"
],
"admin": [
"574f20509f44e7531786c811",
"57508a2a3a0a919c16ace3c0"
],
"teacher": [
"574f20f39f44e7531786c812",
"575002b48188a3f821c2a66e",
"57500bbaea09bc400d047bf6"
],
"student": [
"574f2d56590529c01a2a473b",
"574f2e5842c5885b1b1729ab",
"574f2ed542c5885b1b1729ae",
"574f2f57555210991bf66e07",
"574f2fcd087809b11bd8d5e4",
"574f301d1f5025d61b7392b6",
"574f30481d02afff1bb00c71",
"574f30b01d02afff1bb00c74",
"574f310038136b3d1cf31b96"
]
}
My mongose query
app.services._chkAdminSchool = function(payload){
var deferred = q.defer();
School
//.find({ teacher:{$in:['574f20f39f44e7531786c812']}})
.find({_id : "574f1f979f44e7531786c80f",admin:{"$in":[Object("57508a2a3a0a919c16ace3c0")]}})
//.select(filers)
.exec(function(error, record) {
if (error) {
deferred.reject({
status:404,
message: 'Error in API',
error: error
});
} else {
if(record.length === 0){
deferred.reject({
message: 'Admin and school doesnot match',
data: record
});
}else{
deferred.resolve({
message: 'Successfully get Admin',
data: record
});
}
}
});
return deferred.promise;
}
records are return empty array.
Thanks in advance for help
The query you use works fine for me and returns object, make sure you define admin field in Schema like this:
"admin": [{type: mongoose.Schema.ObjectId }]
to avoid admin array object being string. If you compare Object("57508a2a3a0a919c16ace3c0") to "57508a2a3a0a919c16ace3c0" it will return empty array, check and replay to me, thanks.
I am not sure about how you define your School Schema, but if you are saving id in admin array as String, you don't need to change to Object in your mongoose query.
School.find({
_id: "574f1f979f44e7531786c80f",
admin: {
$in : ['57508a2a3a0a919c16ace3c0']
}
}, functin (err, record) {
...
})

mongoose subdocument sorting

I have an article schema that has a subdocument comments which contains all the comments i got for this particular article.
What i want to do is select an article by id, populate its author field and also the author field in comments. Then sort the comments subdocument by date.
the article schema:
var articleSchema = new Schema({
title: { type: String, default: '', trim: true },
body: { type: String, default: '', trim: true },
author: { type: Schema.ObjectId, ref: 'User' },
comments: [{
body: { type: String, default: '' },
author: { type: Schema.ObjectId, ref: 'User' },
created_at: { type : Date, default : Date.now, get: getCreatedAtDate }
}],
tags: { type: [], get: getTags, set: setTags },
image: {
cdnUri: String,
files: []
},
created_at: { type : Date, default : Date.now, get: getCreatedAtDate }
});
static method on article schema: (i would love to sort the comments here, can i do that?)
load: function (id, cb) {
this.findOne({ _id: id })
.populate('author', 'email profile')
.populate('comments.author')
.exec(cb);
},
I have to sort it elsewhere:
exports.load = function (req, res, next, id) {
var User = require('../models/User');
Article.load(id, function (err, article) {
var sorted = article.toObject({ getters: true });
sorted.comments = _.sortBy(sorted.comments, 'created_at').reverse();
req.article = sorted;
next();
});
};
I call toObject to convert the document to javascript object, i can keep my getters / virtuals, but what about methods??
Anyways, i do the sorting logic on the plain object and done.
I am quite sure there is a lot better way of doing this, please let me know.
I could have written this out as a few things, but on consideration "getting the mongoose objects back" seems to be the main consideration.
So there are various things you "could" do. But since you are "populating references" into an Object and then wanting to alter the order of objects in an array there really is only one way to fix this once and for all.
Fix the data in order as you create it
If you want your "comments" array sorted by the date they are "created_at" this even breaks down into multiple possibilities:
It "should" have been added to in "insertion" order, so the "latest" is last as you note, but you can also "modify" this in recent ( past couple of years now ) versions of MongoDB with $position as a modifier to $push :
Article.update(
{ "_id": articleId },
{
"$push": { "comments": { "$each": [newComment], "$position": 0 } }
},
function(err,result) {
// other work in here
}
);
This "prepends" the array element to the existing array at the "first" (0) index so it is always at the front.
Failing using "positional" updates for logical reasons or just where you "want to be sure", then there has been around for an even "longer" time the $sort modifier to $push :
Article.update(
{ "_id": articleId },
{
"$push": {
"comments": {
"$each": [newComment],
"$sort": { "$created_at": -1 }
}
}
},
function(err,result) {
// other work in here
}
);
And that will "sort" on the property of the array elements documents that contains the specified value on each modification. You can even do:
Article.update(
{ },
{
"$push": {
"comments": {
"$each": [],
"$sort": { "$created_at": -1 }
}
}
},
{ "multi": true },
function(err,result) {
// other work in here
}
);
And that will sort every "comments" array in your entire collection by the specified field in one hit.
Other solutions are possible using either .aggregate() to sort the array and/or "re-casting" to mongoose objects after you have done that operation or after doing your own .sort() on the plain object.
Both of these really involve creating a separate model object and "schema" with the embedded items including the "referenced" information. So you could work upon those lines, but it seems to be unnecessary overhead when you could just sort the data to you "most needed" means in the first place.
The alternate is to make sure that fields like "virtuals" always "serialize" into an object format with .toObject() on call and just live with the fact that all the methods are gone now and work with the properties as presented.
The last is a "sane" approach, but if what you typically use is "created_at" order, then it makes much more sense to "store" your data that way with every operation so when you "retrieve" it, it stays in the order that you are going to use.
You could also use JavaScript's native Array sort method after you've retrieved and populated the results:
// Convert the mongoose doc into a 'vanilla' Array:
const articles = yourArticleDocs.toObject();
articles.comments.sort((a, b) => {
const aDate = new Date(a.updated_at);
const bDate = new Date(b.updated_at);
if (aDate < bDate) return -1;
if (aDate > bDate) return 1;
return 0;
});
As of the current release of MongoDB you must sort the array after database retrieval. But this is easy to do in one line using _.sortBy() from Lodash.
https://lodash.com/docs/4.17.15#sortBy
comments = _.sortBy(sorted.comments, 'created_at').reverse();

Mongoose Insert many to one

I need to help!
I'm creating a website with nodejs and mongo for learning.
I have a problem that I know the best way to do it.
I have two collections codes and tag into table codes I have the tags field is array of tags.
CodeModel:
var CodeSchema = new Schema({
title: { type: 'String', required: true },
text: { type: 'String', required: true },
url: { type: 'String', required: true },
uri: String,
createdAt: { type: Date, default: Date.now },
updatedAt: { type: Date, default: Date.now },
owner: {
type: Schema.ObjectId,
ref: 'User'
},
tags: [
{
type: Schema.ObjectId,
ref: 'Tag'
}
]
});
CodeSchema.pre("save", function (next) {
// if create for first time
if (!this.created_at) {
this.created_at = Date.now();
}
next();
});
module.exports = mongoose.model('Code', CodeSchema);
And My Tag Model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var TagSchema = new Schema({
name: 'string'
});
module.exports = mongoose.model('Tag', TagSchema);
when I get the result in my rest I got it:
[
{
"_id": "5540f557bda6c4c5559ef638",
"owner": {
"_id": "5540bf62ebe5874a1b223166",
"token": "7db8a4e1ba11d8dc04b199faddde6a250eb8a104a651823e7e4cc296a3768be6"
},
"uri": "test-save",
"url": "http://www.google.com.br/",
"text": " hello ",
"title": "testing...",
"__v": 0,
"tags": [
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
},
{
"_id": "55411723fe083218869a82d1",
"name": "JAVA"
}
],
"updatedAt": "2015-04-29T15:14:31.579Z",
"createdAt": "2015-04-29T15:14:31.579Z"
}
]
This I populate into database, I don't know how I insert it, is there any way automatic with mongoose that to do it or I need to create by myself?
I am testing with this json:
{
"url": "http://www.google.com.br/",
"title": "Test inset",
"text": "insert code",
"tags": [
"ANGULAR",
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
}
]
}
I need to do a insert of tags, if I have id or not. Do I need to create it or has way to do it automatically?
and how can I do it?
Sorry my english =x
Generally speaking to create and save a document in a mongo database using mongooseJS is fairly straightforward (assuming you are connected to a database):
var localDocObj = SomeSchemaModel(OPTIONAL_OBJ); // localDocObj is a mongoose document
localDocObj.save(CALLBACK); // save the local mongoose document to mongo
If you have an object that is of the same form as the schema, you can pass that to the constructor function to seed the mongoose document object with the properties of the object. If the object is not valid you will get an invalidation error passed to the callback function on validate or save.
Given your test object and schemas:
var testObj = {
"url": "http://www.google.com.br/",
"title": "Test inset",
"text": "insert code",
"tags": [
"ANGULAR",
{
"_id": "55411700423d29c70c30a8f8",
"name": "GO"
}
]
};
var codeDoc = Code(testObj);
codeDoc.save(function (err, doc) {
console.log(err); // will show the invalidation error for the tag 'Angular'
});
Since you are storing Tag as a separate collection you will need to fetch/create any tags that are string values before inserting the new Code document. Then you can use the new Tag documents in place of the string values for the Code document. This creates an async flow that you could use Promises (available in newer node releases) to manage.
// Create a promise for all items in the tags array to iterate over
// and resolve for creating a new Code document
var promise = Promise.all(testObj.tags.map(function(tag) {
if (typeof tag === 'object') {
// Assuming it exists in mongo already
return tag;
}
// See if a tag already exists
return Tag.findOne({
name: tag
}).exec().then(function(doc) {
if (doc) { return doc; }
// if no tag exists, create one
return (Tag({
name: tag
})).save(); // returns a promise
});
})).then(function(tags) {
// All tags were checked and fetched/created if not an object
// Update tags array
testObj.tags = tags;
// Finally add Code document
var code = Code(testObj);
return code.save();
}).then(function(code) {
// code is the returned mongo document
console.log(code);
}).catch(function(err) {
// error in one of the promises
console.log(err);
});
You can do it like
var checkNewTagAndSave = function(data, doc, next){ // data = req.body (your input json), doc = mongoose document to be saved, next is the callback
var updateNow = function(toSave, newTags){
// save your mongoose doc and call the callback.
doc.set(toSave);
doc.save(next);
};
var data = req.body;
var tagsToCreate = [];
var tagids = [];
data.tags.forEach(function(tag, index){
if(typeof(tag) == 'string') {
tagsToCreate.push({ name: tag });
} else tagids.push(tag._id);
});
data.tags = tagids;
if(tagsToCreate.length === 0) updateNow(data);
else {
mongoose.model('tag').create(tagsToCreate, function(err, models){
if(err || !models) return next(err);
else {
models.forEach(function(model){
data.tags.push(model._id);
});
updateNow(data, models);
}
});
}
};
Hope code is reflecting its logic itself
usage :
after you have found your Code document say aCode
just call
checkNewTagAndSave(req.body, aCode, function(err, doc){
//end your response as per logic
});

Resources