猫鼬查询过滤器,如果填充的字段为空

时间:2020-07-21 15:44:39

标签: mongodb mongoose mongoose-schema mongoose-populate

我有两个分开的猫鼬模型。用户和个人资料。配置文件架构引用了用户架构。

UserSchema

const UserSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
    },
    password: {
      type: String,
      required: true,
    },
  },
  {
    toJSON: { virtuals: true },
    toObject: { virtuals: true },
  }
);

UserSchema.virtual("profile", {
  ref: "profile",
  localField: "_id",
  foreignField: "user",
  justOne: true,
});

那是配置文件架构

const ProfileSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "user",
    },
    image: {
      type: String,
    },
    company: {
      type: String,
    },
    website: {
      type: String,
    },
    start: {
      type: Number,
      required: true,
    },
    graduation: {
      type: Number,
      required: true,
    },
})

我想要查询结果。没有个人资料的用户。我可以用javascript完成。但是我想要它作为mongo查询。我该如何实现?

谢谢

0 个答案:

没有答案