lib.db.model('Stock').collection.aggregate([
{ $match: stockQuery },
{
$lookup: {
from: 'products',
localField: 'product',
foreignField: '_id',
as: 'product'
}
},
{
$project: {
product: {
$arrayElemAt: [ '$product', 0 ]
}
}
},
{ $sort: sort },
{ $limit: req.query.limit },
{ $skip: req.offset }
], callback);
drawApply.find().populate({
path: 'salesId',
select: '_id name phone merchant',
model: 'sales',
populate: {
path: 'merchant',
select: '_id sname',
model: 'merchant'
})
.populate('approver', 'name')
.populate('operator', 'name')
.sort({createTime: -1}).exec(function(err, list) {
// list of drawApplies with salesIds populated and merchant populated
});
Transaction.aggregate([{
$unwind: '$tags'
}, {
$group: {
_id: '$tags',
amount: {
$sum: '$amount'
}
}
}])
.exec(function(err, transactions) {
// Don't forget your error handling
// The callback with your transactions
// Assuming you are having a Tag model
Tag.populate(transactions, {path: '_id'}, function(err, populatedTransactions) {
// Your populated translactions are inside populatedTransactions
});
});
day : {$substr: [{"$add":["$created_at", 28800000]}, 0, 10] }
var data = mongoose.model('test_table', userScheMa); // 与test_table集合关联
exports.query = function(req, res){
var key1 = { carrier : "$carrier" , lang : "$lang"}
var query1 = { '$or' : [{'impCount': {'$gt' : 3}},{'impCount': {'$lt' : 16}}] }
var sort1 = { os: -1 }
var limit1 = 3
var skip1 = 0
data.aggregate(
[
{ $match: query1},
{ $group: { _id: key1 , ImpCount: { $sum: "$impCount" }, ClickCount:{$sum: "$clickCount" }}},
{ $sort: sort1},
{ $limit: limit1 },
{ $skip : skip1}
],function(e,docs)
{
if(e)
console.log(e.message);
res.send(JSON.stringify(docs));
}
);
};
var options={\\\"upsert\\\":false,\\\"multi\\\":false,\\\'new\\\':false}; 不存是否插入,更新是否批量,返回内容是更新前还是后.
var updateJson={};
transaction.findOneAndUpdate(query,updateJson,options,call);
var updateData={};
var newData={};
var updateJson={};
updateJson[\\\"$set\\\"]=updateData;
updateJson[\\\"$setOnInsert\\\"]=newData;
updateJson[\\\"$inc\\\"]={ \\\"points\\\" : 5 };
{ sort : { \\\"points\\\" : 1 } }
mongodb Client https://robomongo.org/download
0 mongodb group values by multiple fields:
http://stackoverflow.com/questions/22932364/mongodb-group-values-by-multiple-fields
http://www.cnblogs.com/shanyou/p/3494854.html
http://mongoosejs.com/docs/2.7.x/docs/schematypes.html
http://ourjs.com/detail/53ad24edb984bb4659000013
http://www.cnblogs.com/anan/archive/2012/06/18/2553754.html
_id: { day: { $dayOfYear: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$date\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}, year: { $year: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$date\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" } },
itemsSold: { $push: { item: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$item\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\", quantity: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$quantity\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\" } }
{upsert: true, new: true,multi: false,},
var conditions = { name: \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'borne\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\' }
, update = { $inc: { visits: 1 }}
, options = { multi: true };
{ $set:
{
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"tags.1\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"rain gear\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\",
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"ratings.0.rating\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": 2
}
$setOnInsert: { defaultQty: 100 }
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$set\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": {
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"history.$.name\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": doc.name,
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"history.$.organisation\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": doc.organisation
},
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"$inc\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": { \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"history.$.score\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": 5 } //increment score
Model.update(conditions, update, options, callback)
db.places.find(
{
location: {
$nearSphere: {
$geometry: {
type : \\\\\\\\\\\\\\\"Point\\\\\\\\\\\\\\\",
coordinates : [ -73.9667, 40.78 ]
},
$minDistance: 1000,
$maxDistance: 5000,
}
}
}
)
spherical: true
mongodb on window
https://dzone.com/articles/install-set-and-start-mongodb
http://www.techoism.com/install-mongodb-3-2-on-windows/
nginx的rewrite的一次演练