user3903448
user3903448

Reputation: 330

Mongoose findOneAndUpdate is not updating the DB

I am learning the Mongoose, GraphQL, Node.js stack. In my application, each Book object has a field similarBookIds to store books similar to that book. I have written a updateBook mutation that takes bookId to which similar books need to be added and similarBookIds i.e. ids of books similar to the book with id bookId.

Initially when I was only trying to add the books one way i.e. if A is similar to B, C, & D; then only update the book A & set it's similarBookIds field to [B, C, D], everything worked just fine. The working code:

updateBook: {
    type: BookType,
    args: {
      bookId: { type: new GraphQLNonNull(GraphQLID) },
      similarBooksIds: { type: new GraphQLList(GraphQLID) },
    },
    resolve(parent, args) {
        if (args.similarBooksIds !== undefined) {
        //   for (const currentbookId of args.similarBooksIds) {
        //     console.log("adding ", args.bookId, " to the set of similar books of ", currentbookId);
        //     Book.findOneAndUpdate(
        //       { _id: currentbookId },
        //       {
        //         $addToSet: {
        //           similarBooksIds: args.bookId,
        //         }
        //       },
        //       { new: true },
        //     );
        //   }

          return Book.findOneAndUpdate(
            { _id: args.bookId },
            {
              $addToSet: {
                similarBooksIds: {
                  $each: args.similarBooksIds
                },
              }
            },
            { new: true },
          );
        }
        return null;
      }
    }
  },

But then I decided to do it both ways i.e. if A is similar B & C, then B is similar to A and C is similar to A. This is where I am facing the issue and the behavior is unexpected. The current state of the code:

updateBook: {
    type: BookType,
    args: {
      bookId: { type: new GraphQLNonNull(GraphQLID) },
      similarBooksIds: { type: new GraphQLList(GraphQLID) },
    },
    resolve(parent, args) {
        if (args.similarBooksIds !== undefined) {
          for (const currentbookId of args.similarBooksIds) {
            console.log("adding ", args.bookId, " to the set of similar books of ", currentbookId);
            Book.findOneAndUpdate(
              { _id: currentbookId },
              {
                $addToSet: {
                  similarBooksIds: args.bookId,
                }
              },
              { new: true },
            );
          }

          return Book.findOneAndUpdate(
            { _id: args.bookId },
            {
              $addToSet: {
                similarBooksIds: {
                  $each: args.similarBooksIds
                },
              }
            },
            { new: true },
          );
        }
        return null;
      }
    }
  },

The only difference in working and not working code is the un-commented part. I also get the output like:-

adding  6316945f8995f05ac71d3b22  to the set of similar books of  63166584a204b075d0aae6a8
adding  6316945f8995f05ac71d3b22  to the set of similar books of  631665b1a204b075d0aae6af
adding  6316945f8995f05ac71d3b22  to the set of similar books of  631695568995f05ac71d3b26
adding  6316945f8995f05ac71d3b22  to the set of similar books of  631695bb8995f05ac71d3b2a
adding  6316945f8995f05ac71d3b22  to the set of similar books of  6319c389a6e51a3ac9a279b2
adding  6316945f8995f05ac71d3b22  to the set of similar books of  6323e8e8225bf0a19a60853a
adding  6316945f8995f05ac71d3b22  to the set of similar books of  63182d151a0a3ed34d549337

But if I check the database, the other way (B similar to A, C similar to A) is not working. (Only the A gets updated with B & C. But B & C doesn't get updated with A each.)

To keep the code minimal, I have removed some things like what other fields does a Book have, how other mutations are written, how does the client calls the mutation, etc.

Let me know if I should add any more details. Thanks in advance.

Upvotes: 1

Views: 48

Answers (0)

Related Questions