Home > other >  I am having difficulty accessing this nested array to carry out an update
I am having difficulty accessing this nested array to carry out an update

Time:12-11

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 }] }
    );

CodePudding user response:

Try with:

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