C
C#2y ago
Ben

❔ MongoDB Update nested elements

MongoDB question, I'm trying to add a property to all of my existing documents. The thing is, my document has a property called Events, this property is an array of Event that might have multiple or 0 elements. I want to update all the nested elements of that property if exists. This is what I tried which doesn't work -
db.collectionName.updateMany(
{Events:{$ne: []}},
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
db.collectionName.updateMany(
{Events:{$ne: []}},
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
First part I'm making sure that events isn't an empty array. Second part I'm adding to all the child elements the NewPropertyTest and initializing it to false. My attempt fails with matchedCount:0. Any tips?
10 Replies
Lexaro
Lexaro2y ago
It looks like the query is incorrect. The $ne operator is used to match all documents where the field is not equal to a specified value. In your case, it should be checking if the field exists, not if it is not equal to an empty array.
db.collectionName.updateMany(
{ "Events": { $exists: true } },
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
db.collectionName.updateMany(
{ "Events": { $exists: true } },
{ $set: { "Events.$[].NewPropertyTest" : false } }
)
This should match all documents that have the "Events" field and then add the "NewPropertyTest" property to all elements in the "Events" array and set its value to false. Let me know if it helped @Ben
Ben
Ben2y ago
Your query will return all the documents. As Events always exists (might be an empty array though). What I did was - Events:{$ne: []} Which basically search for all the documents where events not equal to an empty array. meaning it has nested elements
Lexaro
Lexaro2y ago
This query will return all documents where the Events field is an array with at least one element, but not an empty array.
Ben
Ben2y ago
Are you sure? Iv'e just tested - { "Events": { $exists: true } And it returns documents where Events is empty.
Lexaro
Lexaro2y ago
{ "Events": { $exists: true, $ne: [] } }
{ "Events": { $exists: true, $ne: [] } }
Ben
Ben2y ago
Well just doing {Events:{$ne: []}} covers both 😅 Anyways the select query isn't the issue, its just when I run the update command with the set that it says - matchedCount:0
Lexaro
Lexaro2y ago
db.collection.update({ "Events": { $exists: true, $ne: [] } },
{ $set: { field: value } },
{ multi: true });
db.collection.update({ "Events": { $exists: true, $ne: [] } },
{ $set: { field: value } },
{ multi: true });
Ben
Ben2y ago
same result 😦
Lexaro
Lexaro2y ago
try {
await db.collection.updateMany(
{ "Events": { $exists: true, $ne: [] } },
{ $set: { field: value } }
);
console.log("ur text here");
} catch (err) {
console.error(`ur text here: ${err}`);
}
try {
await db.collection.updateMany(
{ "Events": { $exists: true, $ne: [] } },
{ $set: { field: value } }
);
console.log("ur text here");
} catch (err) {
console.error(`ur text here: ${err}`);
}
@Ben
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
❔ Grapple HookSo I just made a grapple script but I have a gun camera not the actual gun so how do I add the Grapp❔ Custom "block" statement such as while..doHello, is it possible to build a custom block? For example ```csharp // usage DoRequestAsync { ✅ Why am I getting this exception?```cs class algo{ public static void Main(string [] args){ addList.numbers(); i❔ Console.ReadKey() blockingHey there. I have this function that is called every frame: ```public void Update() { if ( !Co❔ Trouble Creating PowerShell CommandletI'm using visual studio 2022, dotnet 6.0. I installed the automation package using > NuGet\Install-P✅ Passing data from controller to viewHi guys hoping for some help here. I have an eCommerce wep app using ASP.NET MVC (7.0) and I want tHow to implement that when you click on the button the data that was sent with it will be displayedHow to implement that when you click on the button the data that was sent with it will be displayed❔ Visual Studio: How to hide tptrace output from Immediate Window during Test Explorer executions?Title says it all. Is there a way to hide the output messages from tptrace that get written to the I❔ Name "PostsService" does not exist in the current context```cs @using BlazorBookGroup.Data . . . private PostsService postsService = new(); protecte❔ System.AccessViolationException: 'Attempted to read or write protected memory.Even with <AllowUnsafeCodeBlocks> enabled and using unsafe { }, this error does not go away. And bef