mongodb 访问此嵌套数组以执行更新时遇到困难

6yjfywim  于 2022-12-12  发布在  Go
关注(0)|答案(1)|浏览(114)

SCHEMA Below is my schema structure, kindly correct me if I am getting it wrong. I want to be able to update the ConnectState from false to true using an ObjectId

phones: {
    type: String,
    required: true,
  },
  User: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user",
    // required: true,
  },
  Userpost: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "userpost",
    // required: true,
  },
  friendshipStatus: [
    {
      isFriend: {
        FProfile: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "profile",
        },
        ConnectStatus: {
          type: Boolean,
          default: false,
        },
      },
    },
  ],
});

What I have tried

I want to update the Boolean value on ConnectStatus from false to true. I know I am getting the process wrong.

const result = await Profile.updateOne(
      { "friendshipStatus.isFriend.FProfile": uid },
      { $set: { "friendshipStatus.$.isFriend.ConnectStatus": true } },
      { arrayFilters: [{ "friendshipStatus.isFriend.FProfile": uid }] }
    );
jucafojl

jucafojl1#

尝试使用:

const result = await Profile.update(
  { 'friendshipStatus.isFriend.FProfile': uid },
  { $set: { 'friendshipStatus.$.isFriend.ConnectStatus': true } },
);

相关问题