How to update documents in mongodb with query

Hi there,

I tried to update some field of documents but i got error.

My document like this

{
        "_id":{
                 "$oid":"62580fb879933461608b499b"
          },
           "buCode":"LMFR",
           "clientNumber":"10002285",
           "audiences":[
          {
           "audience":{
                           "audienceId":"44901",
                           "name":"OFFER_Marketing_Cible_Parcours_SDB_SCORE",
                           "createdDate":{
                            "$date":"2022-03-18T08:00:27.000Z"
                             },
                            "updatedDate":{
                                               "$date":"2022-04-06T10:13:05.000Z"
                            },
                           "maximumNumberOfUseDuringMembership":0,
                          "membershipDuration":1,
                           "category":"LOYALTY",
                           "description":null,
                           "buCode":"LMFR"
                    },
         "startDate":{
            "$date":"2022-04-14T12:12:44.290Z"
          },
         "remainingNumberOfUse":0,
         "lastEntryDate":{
               "$date":"2022-04-14T12:12:44.290Z"
          }
      }
   ]
}

I want to update the field lastEntryDate and startDate to current date.

i write this query but it’s not working. i don’t know why.

db.customer.updateMany
(
	{
	"$and":
		[
			{
			"audiences.audience.audienceId":
				{
				"$eq":"44901"
				}
			},
			{
			clientNumber:
				{
				"$nin":[
				"11803","388223",
				]
				}
			}
		 ]
	},
	{
		$set:
		{
			"audiences.startDate": ISODate(new Date()),
			"audiences.lastEntryDate": ISODate(new Date())
		}
	},
	{
		multi:true
	}
)

The message error is
TypeError: e.match is not a function

Someone can help me

Thanks

Hi @Mohamed_DIABY
Welcome to the community Forum!!

Could you please help me understand the error message in more descriptive way.

Also, I tried using putting the sample data you provided above and query to update the two fields

MongoDB Enterprise atlas-xp4gev-shard-0:PRIMARY> db.sample.updateOne(
     { $and: [
        {"audiences.audience.audienceId": "44901"},
        { "clientNumber" : "10002285" }
 ]
 },
    
        { $set: { "audiences.startDate": new Date(), "audiences.lastEntryDate": "$$NOW" } }
     ]
  )
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }

MongoDB Enterprise atlas-xp4gev-shard-0:PRIMARY> db.sample.find().pretty()
{
	"_id" : ObjectId("625fb215affb7e20b7aae2a6"),
	"buCode" : "LMFR",
	"clientNumber" : "10002285",
	"audiences" : [
		{
			"audience" : {
				"audienceId" : "44901",
				"name" : "OFFER_Marketing_Cible_Parcours_SDB_SCORE",
				"createdDate" : {
					"$date" : "2022-03-18T08:00:27.000Z"
				},
				"updatedDate" : {
					"$date" : "2022-04-06T10:13:05.000Z"
				},
				"maximumNumberOfUseDuringMembership" : 0,
				"membershipDuration" : 1,
				"category" : "LOYALTY",
				"description" : null,
				"buCode" : "LMFR"
			},
			"startDate" : ISODate("2022-04-20T07:41:54.566Z"),
			"remainingNumberOfUse" : 0,
			"lastEntryDate" : ISODate("2022-04-20T07:41:54.676Z")
		}
	]
}

Let us know if you have any further queries.

Thanks
Aasawari

Thanks @Aasawari , i finally resolved the issue.

Mohamed

1 Like