Node.js mongodb将数据插入对象数组并更新它
我需要进行投票,它看起来像一个对象数组,看起来像用户的ID和他设置的值 如果用户已经投票,但更改了其值,则需要更改此用户对象数组中的rate值 我需要创建一个对象数组,将数据插入其中,就像这样{rate:3,user:“asdr2r24f2f42f24”},如果用户已经在这个数组中投票,那么您需要更改给定用户的值速率 我已经试过做点什么了,但在我看来你可以写得更好,你能帮我吗 JSON 模式Node.js mongodb将数据插入对象数组并更新它,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我需要进行投票,它看起来像一个对象数组,看起来像用户的ID和他设置的值 如果用户已经投票,但更改了其值,则需要更改此用户对象数组中的rate值 我需要创建一个对象数组,将数据插入其中,就像这样{rate:3,user:“asdr2r24f2f42f24”},如果用户已经在这个数组中投票,那么您需要更改给定用户的值速率 我已经试过做点什么了,但在我看来你可以写得更好,你能帮我吗 JSON 模式 const mongoose = require('mongoose'); const Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const HabalkaSchema = new Schema({
_id: {
type: String
},
bio: {
firstname: String,
lastname: String,
middlename: String,
company: String
},
rating: [
],
files: [
{
_id: {
type: String
},
destination: {
type: String
},
filename: {
type: String
},
path: {
type: String
},
folder: {
type: String
},
info: {
size: {
type: Number
},
mimetype: {
type: String
},
encoding: {
type: String
},
originalname: {
type: String
},
fieldname: {
type: String
},
},
date: {
type: Date,
default: Date.now
},
bio: {
type: Object
},
userId: String,
guessId: {},
}
],
date: {
type: Date,
default: Date.now
}
});
module.exports = Habalka = mongoose.model('habalka', HabalkaSchema);
这是一个聚合查询,用于插入新用户或更新
评级
数组中现有用户的评级:
示例代码的req.body.id
、req.body.user
和req.body.rating
设置如下:
var ID = 1, INPUT_USER = "new user", INPUT_RATE = 5;
const matchStage = { $match: { _id: ID } };
const facetStage = {
$facet: {
new_user: [
{ $match: { "rating.user": { $not: { $eq: INPUT_USER } } } },
{ $addFields: { rating: { $concatArrays: [ "$rating", [ { user: "new user", rate: INPUT_RATE } ] ] } } },
],
user: [
{ $match: { "rating.user": INPUT_USER } },
{ $addFields: {
rating: {
$map: {
input: "$rating",
as: "r",
in: {
$cond: [ { $eq: [ "$$r.user", INPUT_USER ] },
{ user: "$$r.user", rate: { $add: [ "$$r.rate", INPUT_RATE ] } },
"$$r"
]
}
}
}
} }
]
}
};
const projectStage = {
$project: {
result: { $arrayElemAt: [ { $concatArrays: [ "$user", "$new_user" ] }, 0 ] }
}
};
const queryPipeline = [
matchStage,
facetStage,
projectStage
];
// Run the aggregation query and get the modified document
// after applying the user and rate data in the rating array.
// The result of the aggregation is used to update the collection.
col.aggregate(queryPipeline).toArray( ( err, docs ) => {
console.log("Aggregation output:");
console.log( JSON.stringify( docs[0] ) );
// Update the aggregate result to the collection.
col.updateOne( { _id: docs[0].result._id },
{ $set: { rating: docs[0].result.rating } },
( err, updateResult ) => {
console.log( 'Updated count: ', updateResult.matchedCount );
}
);
callback(docs);
} );
收集文件示例:
如果输入为var ID=1,则输入用户=“新用户”,输入率=5代码>更新后的文档将是:
{ "_id" : 1, "rating" : [ { "user" : "user1", "rate" : 2 }, { "user" : "new user", "rate" : 5 } ] }
{ "_id" : 1, "rating" : [ { "user" : "user1", "rate" : 7 } ] }
如果输入为var ID=1,则输入用户=“用户1”,输入率=5代码>更新后的文档将是:
{ "_id" : 1, "rating" : [ { "user" : "user1", "rate" : 2 }, { "user" : "new user", "rate" : 5 } ] }
{ "_id" : 1, "rating" : [ { "user" : "user1", "rate" : 7 } ] }
谢谢,但这是一个太难的查询,我试过了,但出现了一个错误TypeError:cursor.next不是一个函数=(我更正了代码;它运行和更新很好。聚合查询看起来很复杂,但别无选择。你可以在程序中构建逻辑而不是聚合,但这也会有很多代码。TypeError:Habalka.aggregate(…).toArray不是我使用过的函数,它是MongoDB NodeJS驱动程序API。如果它与您正在使用的不同,则代码将不起作用。