我必须在mongodb聚合pipiline中将毫秒转换为日期格式 -
我的疑问是 -
db.campaign_wallet.aggregate({"$match" : {"campaignId" : 1, "txnTime" : { "$gte" : 1429554600000, "$lte" : 1430159400000}}}, {"$group" : {"_id" : {"msisdn" : "$msisdn", "txnTime" : "$txnTime"}, "count" : {"$sum": 1}}});
Run Code Online (Sandbox Code Playgroud)
在此查询中如何将txnTime(以毫秒为单位)转换为管道中的日期?
我试图使用聚合框架(使用ruby)并像这样投影日期:
db['requests'].aggregate([
{"$project" => {
_id: 0,
method: '$method',
user: '$user',
year: {'$year' => '$timestamp'}
}}])
Run Code Online (Sandbox Code Playgroud)
该文件就像这样:
{
_id: ObjectId("5177d7d7df26358289da7dfd"),
timestamp: ISODate("2013-04-12T03:58:05+00:00"),
method: "POST",
status: "200",
inputsize: "874",
outputsize: "4981",
user: "131"
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Mongo::OperationFailure: Database command 'aggregate' failed: (errmsg: 'exception: can't convert from BSON type EOO to Date'; code: '16006'; ok: '0.0').
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为如果我在与mongorestore导入的完全相同的数据库上运行它,它可以正常工作.
我正在使用mongoose 4.5.0并尝试使用聚合获取数据,但它失败了。
这是我的节点代码:-
..
var preMonths = new moment().subtract(4, 'months').date(1).toDate(); //4:for 5 month
var match = { $and: [] };
match.$and.push({ companyId: new ObjectID(req.user.companyId._id) });
match.$and.push({ status: 'CONVERTED' });
var allUnderUsers = [];
req.session.underUser.forEach(function(element, index) {
allUnderUsers.push(ObjectID(element))
});
match.$and.push({ assignTo: { $in: allUnderUsers } });
match.$and.push({ createdOn: { $gt: preMonths, $lt: new moment().toDate() } });
var aggregate = [
{ $match: match }, {
$group: {
_id: { month: { $month: "$date" },year: { $year: "$date" } }, …Run Code Online (Sandbox Code Playgroud)