Author: leijinmin

The “__v” field in MongoDB

If you don’t need version_key, you can just:

var UserSchema = new mongoose.Schema({
    nickname: String,
    reg_time: {type: Date, default: Date.now}
}, {
    versionKey: false // You should be aware of the outcome after set to false
});

You can also call
resultFromMongo.toObject({ versionKey: false }),
to surppress the value.

In the example of  my lyric site, return the song by suppressing value. 🙂

  Model.findById(req.params.songId)
       .select({title:1,lines:1})
       .exec(function(err,song) {
         if(err)
            sendJsonResponse(res,400,err);
         else {
           song.lines
               .id(req.params.lineId)
               .translation
               .push(req.params.translation);
           song.save(function(error) {
             if(error)
                sendJsonResponse(res,400,error);
             else
               sendJsonResponse(res,200,song.toObject({ versionKey: false }));
           });

         }
       });

C# RegularExpressions: Match.Captures VS Group.Captures

The Group collection contains information only about the last captured substring. About the difference between Match.Captures and Group.Captures, refer to these links:

A problem about the AUTO-implemented properties and the MANUALLY implemented properties

I have to mark this down beacause I lately made a stupid error.  In a little task, I have to inherit some properties in the parent class. I wrote this way:

class Parent
{
private int numéroDeSérie;
private string nomDuNavire;
private int nombreDeMatelots;

public int NuméroDeSérie {get; set;}
public string NomDuNavire {get; set;}
public int NombreDeMatelots {get; set;}
}

When I had to access the properties(NuméroDeSérie,NomDuNavire,NombreDeMatelots) of the Parent class from the inherited Child class, they contained only value null. I found out afterwards that the problem was that the private fields showed no relation to the properties at all. All my action over the privated fields would not be appled to properties, vice versa.

If I have to use the private fields in the parent, I can use the auto-implemented properties as follows:

class Naivre
{
private int numéroDeSérie;
private string nomDuNavire;
private int nombreDeMatelots;

public int NuméroDeSérie
{
get
{
return numéroDeSérie;
}

set
{
numéroDeSérie = value;
}
}

public string NomDuNavire
{
get
{
return nomDuNavire;
}

set
{
nomDuNavire = value;
}
}

public int NombreDeMatelots
{
get
{
return nombreDeMatelots;
}

set
{
nombreDeMatelots = value;
}
}

Otherwise, I should do my action on the public properties themselves in the Parent class and remove the private fields which I thought having a relation to the properties but not in reality.

npm start fails ( Error: listen EADDRINUSE :::3000 )

I deployed my MEAN project on ubuntu. Very often I got the message showing that the port 3000 was in use.

I use the following command to find the pid occupying the port 3000.

sudo netstat -peanut | grep 3000 | awk ‘{ print $9 }’ | sed ‘s:/[^/]*$::’ | grep -Eo ‘[0-9]{1,5}’

In my case, the pid returned is 4532

Then I terminate the process using the command kill.

kill 4532

Finally, npm start succeeds.