Node.js 如何使用mongodb和node js更新和拉取用户
我想使用Mongodb更新并从阵列中提取用户 我正在成功更新阵列中的用户并检查用户长度 如果用户数大于或等于2,我希望对前2个用户进行切片,并使用pull方法将前2个用户从阵列中删除 陌生人聊天连接示例(当两个用户按下“连接陌生人”按钮时连接)Node.js 如何使用mongodb和node js更新和拉取用户,node.js,mongodb,mongoose,mean-stack,mean,Node.js,Mongodb,Mongoose,Mean Stack,Mean,我想使用Mongodb更新并从阵列中提取用户 我正在成功更新阵列中的用户并检查用户长度 如果用户数大于或等于2,我希望对前2个用户进行切片,并使用pull方法将前2个用户从阵列中删除 陌生人聊天连接示例(当两个用户按下“连接陌生人”按钮时连接) 第一个post请求返回状态2错误:未选择用户,因此我想等待一段时间。一旦数组计数增加到2,我需要对其进行切片并从数组中提取所选用户。这将使用等待和承诺组合等待2秒钟 let userName = req.body.userName //wait till
第一个post请求返回状态2错误:未选择用户,因此我想等待一段时间。一旦数组计数增加到2,我需要对其进行切片并从数组中提取所选用户。这将使用
等待和承诺组合等待2秒钟
let userName = req.body.userName
//wait till query result
let docs = await connection.connectedusers.updateMany({}, { $push: { connectArray: [userName] } }, { upsert: true });
//wait till query result
let list = await connection.connectedusers.find({});
//this will wait for 10 secs
await new Promise((resolve, reject) => {
setTimeout(async () => {
resolve(1);
}, 10000);
});
//then process result
if (list[0].connectArray.length >= 2) {
let shortListed = list[0].connectArray.slice(0, 2);
console.log(shortListed,'shortlisted array')
try {
await connection.connectedusers.update({},
{ $pull: { connectArray: { $in: shortListed } } },
{ multi: true }
);
const docs = await connection.connectedusers.find({});
res.json({
message: 'users selected successfully',
status: 1,
docs: docs
});
} catch (err) {
res.status(201).json(err);
}
} else {
const docs = await connection.connectedusers.find({});
console.log(docs, 'docs')
const allUsers = docs.connectArray;
console.log(allUsers, 'allUsers')
await connection.connectedusers.update({},
{ $pull: { connectArray: { $in: allUsers } } },
{ multi: true }
)
res.json({
message: 'users not selected',
status: 2,
docs: docs,
allUsers: allUsers
})
}
用承诺代替异步等待怎么样?我不知道如何使用承诺。这就是我使用async wait的原因。您可以分享一个用户在数据库中的外观示例吗?您在更新之后和设置超时之前立即执行集合查找。如果你想以这种方式组织事情,你需要在设置超时后找到,以确保文档在窗口中。我知道这不是你要求的,但如果我是你,我可能会查看web套接字&其他人是如何组织聊天应用程序的。对我来说,这似乎不是正确的做事方式let sleep=require('util')。promisify(setTimeout)
后跟等待睡眠(10000)
更干净,更适合异步/等待模式。
let userName = req.body.userName
//wait till query result
let docs = await connection.connectedusers.updateMany({}, { $push: { connectArray: [userName] } }, { upsert: true });
//wait till query result
let list = await connection.connectedusers.find({});
//this will wait for 10 secs
await new Promise((resolve, reject) => {
setTimeout(async () => {
resolve(1);
}, 10000);
});
//then process result
if (list[0].connectArray.length >= 2) {
let shortListed = list[0].connectArray.slice(0, 2);
console.log(shortListed,'shortlisted array')
try {
await connection.connectedusers.update({},
{ $pull: { connectArray: { $in: shortListed } } },
{ multi: true }
);
const docs = await connection.connectedusers.find({});
res.json({
message: 'users selected successfully',
status: 1,
docs: docs
});
} catch (err) {
res.status(201).json(err);
}
} else {
const docs = await connection.connectedusers.find({});
console.log(docs, 'docs')
const allUsers = docs.connectArray;
console.log(allUsers, 'allUsers')
await connection.connectedusers.update({},
{ $pull: { connectArray: { $in: allUsers } } },
{ multi: true }
)
res.json({
message: 'users not selected',
status: 2,
docs: docs,
allUsers: allUsers
})
}