Beginner question, query to update multiple records

Hello all,

I am new to MongoDB and like to know how to do a query because I am using Wekan board application that is using MongoDB.

I’ve learned that with the following query I can find boards where I am not an admin

db.getCollection(‘boards’).find({members: {$elemMatch: { userId: “USER-ID-HERE”, isAdmin: false} } })

The query I am looking for is how I can update all boards I am not the admin of, so set it from false to true

Thanks for helping.

Please provide sample documents from you collection and the expected results.

Coming up with documents that match your use-case in order to experiment is time consuming.

Please share anything you tried and indicate how it fails so that we do not experiment and propose a solution that you already rejected.

As a starting point look at updateMany because that will be the method to use rather than find().

The query parameter of updateMany() will be the same as your find().

The update parameter will be $set.

Of particular interest for your array update is https://www.mongodb.com/docs/manual/reference/method/db.collection.updateMany/#specify-arrayfilters-for-an-array-update-operations. However $ positional update might be sufficient in your case.

Hello @steevej
Thank you for helping me

I am not sure what you mean with sample documents? Do you mean a sample of the Wekan application? If so the developer provided one at https://boards.wekan.team/b/D2SzJKZDS4Z48yeQH/wekan-open-source-kanban-board-with-mit-license .

This is not the Wekan instance I am trying to change because that is not an open instance.
So I am trying to change all boards I am member of, but don’t have admin.
I didn’t tried any update / manyupdate query yet because I am too new to it and afraid I am doing something wrong. But if I do understand you well I can do:

db.getCollection(‘boards’).updateMany({members: {$elemMatch: { userId: “USER-ID-HERE”, isAdmin: false} } })
And need to change USER-ID-HERE with my user id
But where and how exactly in this line should I add the $set for isAdmin true ?

Best regards,
Jurjen

You are trying to update documents from the collection boards. We need to see the structure of those documents. And we need sample documents that we can cut-n-paste into out system to play with.

Thanks for the link but most of us have to time to go investigate how a third party software you want to use integrate with the database. If you know that documents from the boards collection have an array named members which contains object with the fields userId and isAdmin you must have seen at least one document.

Just doing

will provide sample documents.

Hello @steevej

Sorry I misunderstood what you meant with sample documents. I only know the term records for what is returned by a query into databases.

This is a paste of 2 rows records/documents with the query. I changed the userids because I am not sure if it is sensitive information for a public forum.

So in this records/documents and all others that I got from the query should the isAdmin: false be updated where userId is xxx
I like to know how to build that query in MongoDB.

Thanks again :blush:

db.getCollection(‘boards’).find({members: {$elemMatch: { userId: “xxx”, isAdmin: false} } })
{ “_id” : “L4iFMFcj2QSLM8aJM”, “title” : “EU epics”, “permission” : “private”, “sort” : 11, “slug” : “eu-epics”, “archived” : true, “createdAt” : ISODate(“2021-02-24T14:07:33.070Z”), “modifiedAt” : ISODate(“2021-02-24T14:09:29.773Z”), “stars” : 0, “labels” : [ { “color” : “green”, “_id” : “v4HA7q”, “name” : “” }, { “color” : “yellow”, “_id” : “cPCsBe”, “name” : “” }, { “color” : “orange”, “_id” : “bYLMsW”, “name” : “” }, { “color” : “red”, “_id” : “yBfFjM”, “name” : “” }, { “color” : “purple”, “_id” : “EWiMFB”, “name” : “” }, { “color” : “blue”, “_id” : “bhpW2h”, “name” : “” } ], “members” : [ { “userId” : “other1”, “isAdmin” : true, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “xxx”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “ytdWuptdA2aJEpSqw”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false } ], “color” : “belize”, “subtasksDefaultBoardId” : null, “subtasksDefaultListId” : null, “dateSettingsDefaultBoardId” : null, “dateSettingsDefaultListId” : null, “allowsSubtasks” : true, “allowsAttachments” : true, “allowsChecklists” : true, “allowsComments” : true, “allowsDescriptionTitle” : true, “allowsDescriptionText” : true, “allowsActivities” : true, “allowsLabels” : true, “allowsAssignee” : true, “allowsMembers” : true, “allowsRequestedBy” : true, “allowsAssignedBy” : true, “allowsReceivedDate” : true, “allowsStartDate” : true, “allowsEndDate” : true, “allowsDueDate” : true, “presentParentTask” : “no-parent”, “isOvertime” : false, “type” : “board”, “archivedAt” : ISODate(“2021-02-24T14:09:29.684Z”), “allowsCardNumber” : false, “allowsShowLists” : true }
{ “_id” : “GJXMEnZqrTPuAkyvz”, “title” : “Top Priorities”, “permission” : “private”, “sort” : 71, “slug” : “top-priorities”, “archived” : false, “createdAt” : ISODate(“2021-11-25T11:17:40.437Z”), “modifiedAt” : ISODate(“2022-04-29T08:30:06.646Z”), “stars” : 1, “members” : [ { “userId” : “other2”, “isAdmin” : true, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other3”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other4”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “xxx”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other5”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other6”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other7”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other8”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other9”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other10”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false } ], “color” : “belize”, “subtasksDefaultBoardId” : null, “subtasksDefaultListId” : null, “dateSettingsDefaultBoardId” : null, “dateSettingsDefaultListId” : null, “allowsSubtasks” : true, “allowsAttachments” : true, “allowsChecklists” : true, “allowsComments” : true, “allowsDescriptionTitle” : true, “allowsDescriptionText” : true, “allowsCardNumber” : false, “allowsActivities” : true, “allowsLabels” : true, “allowsCreator” : false, “allowsAssignee” : true, “allowsMembers” : false, “allowsRequestedBy” : true, “allowsCardSortingByNumber” : true, “allowsAssignedBy” : false, “allowsReceivedDate” : true, “allowsStartDate” : true, “allowsEndDate” : true, “allowsDueDate” : true, “presentParentTask” : “no-parent”, “isOvertime” : false, “type” : “board”, “allowsShowLists” : true }

Please read Formatting code and log snippets in posts and update the same documents (records) so that we can cut-n-paste into our system.

Like this?

{ “_id” : “L4iFMFcj2QSLM8aJM”, “title” : “EU epics”, “permission” : “private”, “sort” : 11, “slug” : “eu-epics”, “archived” : true, “createdAt” : ISODate(“2021-02-24T14:07:33.070Z”), “modifiedAt” : ISODate(“2021-02-24T14:09:29.773Z”), “stars” : 0, “labels” : [ { “color” : “green”, “_id” : “v4HA7q”, “name” : “” }, { “color” : “yellow”, “_id” : “cPCsBe”, “name” : “” }, { “color” : “orange”, “_id” : “bYLMsW”, “name” : “” }, { “color” : “red”, “_id” : “yBfFjM”, “name” : “” }, { “color” : “purple”, “_id” : “EWiMFB”, “name” : “” }, { “color” : “blue”, “_id” : “bhpW2h”, “name” : “” } ], “members” : [ { “userId” : “other1”, “isAdmin” : true, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “xxx”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “ytdWuptdA2aJEpSqw”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false } ], “color” : “belize”, “subtasksDefaultBoardId” : null, “subtasksDefaultListId” : null, “dateSettingsDefaultBoardId” : null, “dateSettingsDefaultListId” : null, “allowsSubtasks” : true, “allowsAttachments” : true, “allowsChecklists” : true, “allowsComments” : true, “allowsDescriptionTitle” : true, “allowsDescriptionText” : true, “allowsActivities” : true, “allowsLabels” : true, “allowsAssignee” : true, “allowsMembers” : true, “allowsRequestedBy” : true, “allowsAssignedBy” : true, “allowsReceivedDate” : true, “allowsStartDate” : true, “allowsEndDate” : true, “allowsDueDate” : true, “presentParentTask” : “no-parent”, “isOvertime” : false, “type” : “board”, “archivedAt” : ISODate(“2021-02-24T14:09:29.684Z”), “allowsCardNumber” : false, “allowsShowLists” : true }
{ “_id” : “GJXMEnZqrTPuAkyvz”, “title” : “Top Priorities”, “permission” : “private”, “sort” : 71, “slug” : “top-priorities”, “archived” : false, “createdAt” : ISODate(“2021-11-25T11:17:40.437Z”), “modifiedAt” : ISODate(“2022-04-29T08:30:06.646Z”), “stars” : 1, “members” : [ { “userId” : “other2”, “isAdmin” : true, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other3”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other4”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “xxx”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other5”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other6”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other7”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other8”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other9”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false }, { “userId” : “other10”, “isAdmin” : false, “isActive” : true, “isNoComments” : false, “isCommentOnly” : false, “isWorker” : false } ], “color” : “belize”, “subtasksDefaultBoardId” : null, “subtasksDefaultListId” : null, “dateSettingsDefaultBoardId” : null, “dateSettingsDefaultListId” : null, “allowsSubtasks” : true, “allowsAttachments” : true, “allowsChecklists” : true, “allowsComments” : true, “allowsDescriptionTitle” : true, “allowsDescriptionText” : true, “allowsCardNumber” : false, “allowsActivities” : true, “allowsLabels” : true, “allowsCreator” : false, “allowsAssignee” : true, “allowsMembers” : false, “allowsRequestedBy” : true, “allowsCardSortingByNumber” : true, “allowsAssignedBy” : false, “allowsReceivedDate” : true, “allowsStartDate” : true, “allowsEndDate” : true, “allowsDueDate” : true, “presentParentTask” : “no-parent”, “isOvertime” : false, “type” : “board”, “allowsShowLists” : true }```

No not like this.

Most likely you cut-n-paste from your previous post, so you got the wrong quotes. Exactly, what the triple dots fencing is supposed to prevent.

To know if it is correct or not. You should try to copy the result of your editing, the part in the right pane when you edit post, and paste it into mongosh. If it works for you it will work for us, if it does not for you it will not for us.

What we get when we try to cut-n-paste is:

SyntaxError: Unexpected character '“'. (1:6)

Sorry for my very late response. Today I had a friend Rafid helping me with this. For who is in need for a similar query, here is the one that worked for me.

db.boards.updateMany(
{ members: { $elemMatch: { userId: “USER-ID-HERE”, isAdmin: false } } },
{
$set: { “members.$.isAdmin”: true },
}
);

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.