OrderId field populated by Trigger does not appear on returned object from initial save(). I'm forced to retrieve object multiple times before field appears

Hi,

I’m prototyping an node.js app, using the free tier, using Mongoose library (I don’t think this is the cause of the issue though).

I followed instructions here:

to create an auto increment orderId field in my orders collection documents upon create.

When I save() the object, I get the object back from the call, the orderId is not present.

If I go check the the object in Atlas, it is there. I decided then to retrieve the object by Id again, to see if it would appear. It would appear some times.

I then retrieved the object a 2nd time, and it would appear more often.

I then retrieved the object a 3rd time, and now I see the orderId consistently.

I’m not familiar with the workings of mongodb and how they do these triggers, so I assume there is some sort of timing issue.

Upon save I’m returned the object before the trigger has executed. I tried to do a {j; true} option to see if it would return properly, but was not successful.

I saw that if the trigger is called a lot, it might be put on an execution queue, but I’m just testing out the api, so that should not be the issue.

Could it be because I’m on the free tier?

Am I missing something here?

Thanks for your help,
Rich

newOrder = await newOrder.save();

  //console.log(newOrder);

  console.log("ID: " + newOrder.id);
  console.log("orderId 1: " + newOrder.orderId);

  let updatedOrder = await Order.findById(newOrder.id).populate({
    path: 'orderItems',
    populate: { path: 'menuItem', select: "name" }//description
  });

  console.log("orderId 2: " + updatedOrder.orderId);

  updatedOrder = await Order.findById(newOrder.id).populate({
    path: 'orderItems',
    populate: { path: 'menuItem', select: "name" }//description
  });

  updatedOrder = await Order.findById(newOrder.id).populate({
    path: 'orderItems',
    populate: { path: 'menuItem', select: "name" }//description
  });

  console.log("orderId 3: " + updatedOrder.orderId);

Output

ID: 61f8358055301a3970af47f4

orderId 1: undefined

orderId 2: undefined

orderId 3: 68

Also, to double check that it was not an issue with Mongoose, I went ahead and added some test code that used the mongodb node driver directly. I see the same issue. orderId is undefined when I retrieve the order immediately after inserting it. If I check atlas, I can see it has one. I suspect if I keep retrieving the object, it will eventually appear.

Rich

const client = new MongoClient(uri);

  await client.connect();
  const database = client.db('ordering');
  const orders = database.collection('orders');

  const testOrder = { location: 3, completed: false };
  
  const result = await orders.insertOne(testOrder);

  const newId = result.insertedId;

  console.log("object Id: " + newId);

  const query = { _id: newId };
  const latestOrder = await orders.findOne(query);

  console.log("orderId test: " + latestOrder.orderId);
object Id: 61f842807d4e78c006ca75a3
orderId test: undefined

Additionally, I tried doing muliple lookups and you can see the behavior varies.

Thanks,
Rich

object Id: 61f843ba2d5c561249524ec9

orderId test: undefined

orderId test: 82

orderId test: 82

orderId test: 82

orderId test: 82

object Id: 61f843c82d5c561249524f65

orderId test: undefined

orderId test: undefined

orderId test: undefined

orderId test: 84

orderId test: 84

Anyone have any clue on this? Support from Mongo said they’d get back to me. Haven’t heard from them.

As an experiment, I upgraded my cluster to an M10, thinking that this would resolve the issue. I thought maybe because it was underpowered, the trigger was not working in a timely fashion.

It did not.

I don’t understand how mongo can return the document upon insert to me with the value NOT.

Am I missing something here?

Thanks for any help,
Rich

That should be:

I don’t understand how mongo can return the document upon insert to me with the value NOT set.

Did you ever figure this out? I’m having the same issue. Thanks.

I never did resolve this issue. I ended up not using the auto-increment feature. I find it really weird that this function is not working.

Every SQL DB that I know of can us an auto increment function for creating primary keys (Identity). I would think this is something that MongoDB could handle as well.