【问题标题】:how to make a mongoose query to search for matching arrays如何进行猫鼬查询以搜索匹配的数组
【发布时间】:2019-06-05 19:19:26
【问题描述】:

我正在制作一个聊天应用程序,用户第一次向某人发送消息时会在数据库中创建一个新对话,但在每次创建新对话之前,我想检查对话是否存在,我我在 Node.js 中构建它并使用 express 框架 & 我正在使用 Mongoose 来处理数据库

const mongoose = require('mongoose');

const Conversation = new mongoose.Schema({
    Users : [
        {
            user_id : {
                type : String,
                required : true
            },
            username : {
                type : String,
                required : true
            },
            name : {
                type : String,
                required : true
            },
            profileUrl : {
                type : String,
                required : true
            }
        }
    ],
    createdAt : {
        type : Date,
        default : Date.now,
        required : true
    }

});


module.exports = mongoose.model("Conversation",Conversation); 

是否有一个查询,我可以检查是否存在与我传递的数组匹配的对话,我找不到用对象搜索数组的方法,我希望所有字段都相等,而不仅仅是一个人

【问题讨论】:

  • NodeJS 中的聊天应用程序?你在使用 Socket.IO 吗?
  • 是的,我正在使用 socket.io 和 rabbitmq

标签: node.js mongodb rest express mongoose


【解决方案1】:

//创建回调函数 module.exports.FindMessage= 函数(条件查询,回调){ 用户 .find(条件查询,回调) }

//比

User.FindMessage({$or:[{username:username}]},function(err,message){ if(err) 抛出错误 如果(消息长度 > 0 ) {

        }
        else
        {
         }

【讨论】:

    猜你喜欢
    • 2019-08-07
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多