小编Abt*_*med的帖子

MongoDB组聚集,条件为$ sum

我的数据库集合如下所示

[{ 
   _id:1,
   status:"active",
   sale: 4,
   createdAt:"2019-10-08 08:46:19"
},
{
   _id:2,
   status:"inactive",
   sale:5,
   createdAt:"2019-10-08 06:41:19"
},
{
   _id:2,
   status:"inactive",
   sale:5,
   createdAt:"2019-10-08 02:01:19"
}]
Run Code Online (Sandbox Code Playgroud)

我需要按“天”分组。我想要的结果

[{
  createdAt:"2019-10-08 02:01:19",
  inactive: 2,
  active:1,
  salesOfActive: 4,
  salesOfInactive:10
}]
Run Code Online (Sandbox Code Playgroud)

我没有得到希望得到任何帮助的实际结果,我们将不胜感激

我已经尝试过此方法,但不知道如何每天获得salesOfActivesalesOfInactive

{
        $group: {
          _id: {
            day: { $dayOfMonth: "$createdAt" }
          },
          inActive:{$sum: { status:"inactive"}},
          active:{$sum: { status:"active"}},
          salesOfActive: { $sum:$sale  }
        }
}
Run Code Online (Sandbox Code Playgroud)

javascript mongoose mongodb mongodb-query aggregation-framework

5
推荐指数
1
解决办法
61
查看次数

使用node、react和socket.io出现Socket.io cors错误

我们使用 CORS 来允许所有来源

app.use(cors());
Run Code Online (Sandbox Code Playgroud)

服务器运行在4000端口,客户端运行在3000端口

这是我的server.js代码

const cors = require("cors");
const http = require("http");
const socketIO = require("socket.io");

app.use(cors());

const port = process.env.PORT || process.env.DEFAULT_PORT;
console.log("port: ", port);
app.listen(port, () => {
  console.log(`App listening at ${port}...`);
});

const server = http.createServer(app);

const io = new socketIO(server, {
  transports: ["websocket"],

});
Run Code Online (Sandbox Code Playgroud)

反应js代码

  constructor(props) {
    super(props);
    try {
      this.socket = io("http://localhost:4000", { transport: ["websocket"] });

      this.socket.on("Chat-event", data => {
        console.log("socketdata", data);
      });
    } catch (error) {
      console.log("hiterror", error)
    }
  } …
Run Code Online (Sandbox Code Playgroud)

sockets node.js reactjs

0
推荐指数
1
解决办法
5773
查看次数