UpdateOne in Node

I’m having a bit of an issue with my node js application where I’m hitting my server to run a nested updateOne query and getting a response back where mongo is telling me that it doesn’t find a document that matched my query to update.

I’ve run the same query by hitting the API through a command line, and the document updates perfectly. So far I’ve tried changing the data types for all combinations of elements, and the mongo function to be “find” rather than “updateOne”. I’ve also tried changing the “addToSet” to “set”, and altering the parameters, but haven’t gotten anywhere.

Been pulling my hair out over this for a few days, so any suggestions are much appreciated

Code:

    app.post("/xx/yy/:item1/:item2", function (request, response) {
        EndpointA = "EndpointA"
        EndpointB = "EndpointB"
        ItemLookup = database.collection(MongoInterpret(EndpointB))
        ItemUpdate = database.collection(MongoInterpret(EndpointA))
        apple = request.body.item1
        ItemLookup.find({"_id": apple}).toArray((error, result) => {
            if (error) {
                return response.status(500).send(error)
            }
            const tempJson = result[0]
            importObj = {}
            importObj['_id'] = tempJson.apple
            importObj['name'] = tempJson.pear
            ItemUpdate.updateOne(
                { "listID": request.params.item2},
                { $addToSet: { "DocDetails": importObj } },
                function(error, result) {
                    if (error) {
                        console.log(error)
                        return response.status(500).send(error);
                    };
                    console.log(result)
                    response.send(result);
                }
            )
            return
        })
    });

Response using application:

    result: {
        n: 0,
        nModified: 0,
        opTime: { ts: [Timestamp], t: 30 },
        electionId: 7fffffff000000000000001e,
        ok: 1,
        '$clusterTime': { clusterTime: [Timestamp], signature: [Object] },
        operationTime: Timestamp { _bsontype: 'Timestamp', low_: 2, high_: 1598328571 }
      }.......

Response using command line:

    result: {
        n: 1,
        nModified: 1,
        opTime: { ts: [Timestamp], t: 30 },
        electionId: 7fffffff000000000000001e,
        ok: 1,
        '$clusterTime': { clusterTime: [Timestamp], signature: [Object] },
        operationTime: Timestamp { _bsontype: 'Timestamp', low_: 3, high_: 1598331308 }
      }.......

Hey Kent,

Were you able to figure out this problem? Since n is 0, I’m wondering if MongoDB can’t find a document that matches your filter. Have you tried printing/debugging request.params.item2 to confirm it matches the listId of a document in your collection? You could also try running a findOne({}) on the ItemUpdate collection to make sure you are connecting and can retrieve anything from the collection.

1 Like