繁体   English   中英

如何通过 nodeJS 在 mongoDB 中更新 object?

[英]How do i update a object in mongoDB via nodeJS?

我正在尝试这样做,以便当玩家获得高分时,它会在那里搜索名称,然后将该高分添加到那里的帐户中。 我的问题是我不知道如何使用 mongoose 搜索名称和更新标签?

这是我的服务器代码:

var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var express = require("express");
var app = express();
var http = require("http").createServer(app);
var io = require("socket.io")(http);

var PORT = 3332;

app.use("/", express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect("mongodb://localhost/endg", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
var db = mongoose.connection;
db.once("open", function (cb) {
  console.log("connection established");
});

io.on("connection", function (socket) {
  console.log("user connected");

  socket.on("chooseName", function (newName) {
    var data = {
      nickname: newName,
      highscore: 0,
    };
    db.collection("dat").findOne({ nickname: data.nickname }, function (
      err,
      doc
    ) {
      if (err) throw err;
      if (doc) {
        io.emit("nnTaken", null);
      } else {
        db.collection("dat").insertOne(data, function (err, coll) {
          if (err) throw err;
          console.log("rec estab");
          io.emit("newNickname", null);
        });
      }
    });
  });

  socket.on("player", function (player) {
    socket.on("highscore", function (hs) {
      console.log(player + ": " + hs);
      db.collection("dat").updateOne(
        { name: player },
        { $set: { highscore: hs } }
      );
      //This is where im trying to update but the above code does not work
    });
  });
});

http.listen(PORT, function () {
  console.log("server is up and running using port " + PORT);
});

我该怎么做? 我尝试在高分套接字内使用更新,这样当获得高分时,它会更新该字段但没有任何变化。

我真的想通了。 如果将来有人遇到此问题,我将发布答案。

var mongoose = require("mongoose");
var bodyParser = require("body-parser");
var express = require("express");
var app = express();
var http = require("http").createServer(app);
var io = require("socket.io")(http);

var PORT = 3332;

app.use("/", express.static(__dirname));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

mongoose.connect("mongodb://localhost/endg", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});
var db = mongoose.connection;
db.once("open", function (cb) {
  console.log("connection established");
});

io.on("connection", function (socket) {
  console.log("user connected");

  socket.on("chooseName", function (newName) {
    var data = {
      nickname: newName,
      highscore: 0,
    };
    db.collection("dat").findOne({ nickname: data.nickname }, function (
      err,
      doc
    ) {
      if (err) throw err;
      if (doc) {
        io.emit("nnTaken", null);
      } else {
        db.collection("dat").insertOne(data, function (err, coll) {
          if (err) throw err;
          console.log("rec estab");
          io.emit("newNickname", null);
        });
      }
    });
  });

  socket.on("player", function (player) {
    socket.on("highscore", function (hs) {
      console.log(player + ": " + hs);
      db.collection("dat").findOne({ nickname: player }, function () {
        db.collection("dat").updateOne(
          { nickname: player },
          { $set: { highscore: hs } }
        );
      });
    });
  });
});

http.listen(PORT, function () {
  console.log("server is up and running using port " + PORT);
});

我继续,基本上是使用 findOne 来查找球员姓名,然后使用带有 $set 的 updateOne 来更新高分值。

我猜这个问题源于 mongo 无法判断我试图更新哪个值。

或者,如果您更喜欢带有 es6 的异步 function

socket.on('highscore', async highscore => {
  await db.collection('dat').findOneAndUpdate(
    { nickname: player },
    { $set: { highscore: hs } }
  )
});

我正在使用 ES6 Javascript 语法。 highscore => {}function (highscore)
我也在使用异步 function。 await表示程序等到 function 完成

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM