These are chat archives for Automattic/mongoose

Mar 2017
Mustafa Dur
Mar 16 2017 13:30 UTC
I have users and and users have documents. Users can have maximum 100 documents. Users can only edit maximum 50 documents. When I am inserting a new document, first I query User.documents to find number of documents and then I query Documents with userId and isFinished=true and check if there is a room for editable document. Is there a way to get both total and finished documents of the user with a single query? My current code is something like below.
var UserSchema = mongoose.Schema({
    email: {type: String, index: {unique: true, sparse: true}},
    password: String,

    ref: 'Document',

var DocumentSchema = mongoose.Schema({
    owner : { type:mongoose.Schema.Types.ObjectId, ref: 'User' },
    name: String,
    data: String,
    isFinished: {
        type: Boolean, default: false
    updateDate: Date

 User.findOne({_id: req.userId}).populate('documents').exec(function(err, user) {

 // user.documents == total user documents

if (user.documents && user.documents.length < 100) {

  Document.count({owner:req.userId,isFinished:true},function(err,count) {
  // count == finished user documents
        if (count < 50) {

            // do something