gpt4 book ai didi

node.js - 更新 MongoDB 集合 - JavaScript

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:16 25 4
gpt4 key购买 nike

我正在尝试更新集合中的值。用户单击一个按钮,与该按钮对应的数字将被发送到服务器以添加到集合中。

我无法更新集合,但如果我使用db.orders.update(),它在控制台中工作正常

订单型号:

// DB Initiation stuff

var orderSchema = new mongoose.Schema({
status: String,
rated: Number
});

var collection = 'orders';
var Order = db.model('Order', orderSchema, collection);

module.exports = Order;

客户端(点击按钮时)

// starID = 5; id = 50e6a57808a1d92dcd000001
socket.emit('rated', {rated: starID, id: id});

socket.js:

var Order = require('../models/orders');

socket.on('rated', function(data) {
/* Probably a better way to do this but, wrap
* the id in 'ObjectId(id)'
*/
var id = 'ObjectId("'+data.id+'")';
Order.update( {_id: id}, {$set: {rated: data.rated}} );
socket.emit('updated', {
note: 'Some HTML notification to append'
});
});

最佳答案

让 Mongoose 将 id 字符串转换为 ObjectId:

socket.on('rated', function(data) { 
Order.update( {_id: data.id}, {$set: {rated: data.rated}} );
socket.emit('updated', {
note: 'Some HTML notification to append'
});
});

关于node.js - 更新 MongoDB 集合 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14157846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com