These are chat archives for Automattic/mongoose

17th
Mar 2017
Paul "Joey" Clark
@joeytwiddle
Mar 17 2017 02:35
@tosbaha We usually approach that by making two queries in parallel, and waiting for both to complete, using promises:
const getUser = User.findOne({_id: req.userId}).populate('documents’);
const countFinishedDocs = Document.count({owner: req.userId, isFinished: true});

Promise.all([getUser, countDocs]).then(results => {
  const user = results[0];
  const finishedDocCount = results[1];
  if (!user) throw Error("User not found");

  if (user.documents.length > 100 && finishedDocCount < 50) {
    // do something
  }
}).catch(console.error);
Mustafa Dur
@tosbaha
Mar 17 2017 09:29
Thanks @joeytwiddle I just wonder if there is single query to get what I want. Your version again makes two queries but with promises. I think I have found something but I am not so sure about it. I will write my version if no one else comments further.
Paul "Joey" Clark
@joeytwiddle
Mar 17 2017 09:36

@tosbaha Do you really need to populate the documents field? If it was an Array of ObjectIds, then you could check the length without populating.

@tosbaha Alternatively, can you get all the information you need from the Document collection? If so, you might be able to use an aggregation to sum the count-of-finished and count-of-all in one pass.

But in my project, I would probably just make two easy queries, and worry about optimizing later, if it’ s needed. ;)

There are some aggregations with conditional sums here.
Felix Itzenplitz
@cebor
Mar 17 2017 09:51
Which is the common way to build a recursive schema ?
Mustafa Dur
@tosbaha
Mar 17 2017 09:54
@joeytwiddle I don't need the documents. I just need total and finished documents count. So that I can allow user to add further documents. I am using virtual reference which is a new feature of mongoose. So that actually documents Id are not stored in user but virtually linked. My current code is below.
            Document.aggregate([
                {
                    $match: {
                        owner:user._id
                    }},
                { $group: {
                    "_id": "$owner",
                    "totalCount": {
                        $sum: 1
                    },
                    "finishedCount" : { $sum:  { $cond: ["$isFinished",1,0]} }
                }
                }]).exec(function (err,result) {
                if ( result && result.length > 0) {
                    if ( result[0].totalCount >= config.registeredTotalLimit) {
                        // Registered total limit is reached
                        console.log('Register registeredTotalLimit');
                    }
                    if (result[0].finishedCount >= config.registeredActiveLimit) {
                        // Registered active limit is reached
                        console.log('Register registeredActiveLimit');
                    }
                }
Paul "Joey" Clark
@joeytwiddle
Mar 17 2017 09:55
Nice, that’s what I was thinking too.
martijnn
@martijnn
Mar 17 2017 11:54

Hi there,
Got a question concerning schema design: when should I be using an objectref and when should I choose to include the subdoc in the schema intself?

'webdomains': [{
    type: Schema.Types.ObjectId,
    ref: 'Webdomain'
  }]

vs

'webdomains': [Webdomain]
Anton M.
@bookin
Mar 17 2017 13:09
Hello anyone. Need help, often get error "MongoError: connection 1 to localhost:27017 timed out", how I can fix it? Do I need change "socketTimeoutMS"? Is is default 30 second? If I set 0 it will be 0milisecond or waite without limitation? Any help, thanks.
Christian Roy
@roychri
Mar 17 2017 14:43
@bookin Is that error intermittent or persistent?
Anton M.
@bookin
Mar 17 2017 15:04
intermittent
Christian Roy
@roychri
Mar 17 2017 15:05
Did you check mongo logs for any hints about the error?
Anton M.
@bookin
Mar 17 2017 15:06
I haven't any logs now, folder /var/logs/mongodb is empty