Javascript MongoDB无法获取所有数据 问题
我想从我的MongoDB集合中获取所有记录,这些记录在Javascript MongoDB无法获取所有数据 问题,javascript,mongodb,mongoose,Javascript,Mongodb,Mongoose,我想从我的MongoDB集合中获取所有记录,这些记录在from或to字段中有用户名或其他名称。这是一个聊天应用程序。我想获取用户和其他用户之间的所有聊天记录。这是一种模式,当用户发送消息时,其用户名将保存在from部分,当用户收到消息时,其用户名将保存在to部分。对于其他用户也是如此 请参阅以下内容: 论| |在句法上的使用 输出 关于&&运算符的使用 输出 客观的 我想获取name参数中给定用户和登录用户之间的所有记录。例如,在Jahad和Mazino之间。只需尝试使用$或操作符,如: c
from或to
字段中有用户名或其他名称。这是一个聊天应用程序。我想获取用户和其他用户之间的所有聊天记录。这是一种模式,当用户发送消息时,其用户名将保存在from
部分,当用户收到消息时,其用户名将保存在to
部分。对于其他用户也是如此
请参阅以下内容:
论| |在句法上的使用
输出
关于&&运算符的使用
输出
客观的
我想获取
name
参数中给定用户和登录用户之间的所有记录。例如,在Jahad和Mazino之间。只需尝试使用$或操作符,如:
const messages = await Chat.find({
from: {$or: [otherUser.username, user.username]},
to: {$or: [otherUser.username, user.username]}
}).sort({createdAt: -1})
请不要粘贴图像,以格式化文本的形式提供代码和错误:这会导致错误无法使用$或字符串。。所以我只是删除了$or操作符,它就成功了。非常感谢。
getMessages: async(_, { name }, context) => {
const user = checkAuth(context);
if (!user) throw new AuthenticationError('Not logged in');
const otherUser = await User.findOne({username: name})
if (!otherUser) throw new UserInputError('User Input Error');
const messages = await Chat.find({ // Here is the change
from: otherUser.username && user.username,
to: user.username && otherUser.username
}).sort({createdAt: -1})
return messages
}
const messages = await Chat.find({
from: {$or: [otherUser.username, user.username]},
to: {$or: [otherUser.username, user.username]}
}).sort({createdAt: -1})