Mongo Update $set not working - mongodb

I'm trying to mass update some mongo documents.
I'm using the query
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set :
{
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
}
)
This query does not update the source.expires-at field, however the where part of the statement works fine.
The document structure is
{
"_id": ObjectId("5211dc100000044707000015"),
"categories": {
"0": {
"id": ObjectId("51cd5272222wb6zs464fa4d9")
}
},
"source": {
"importer": "pa",
"expires-at": ISODate("2013-09-18T08:49:32.0Z")
}
}

Try this:
db.articles.update(
{
'categories.id': ObjectId("51cd5272222wb6zs464fa4d9"),
'source.importer': 'pa'
},
{
$set: {
'source.expires-at': ISODate("2014-01-01T08:39:45Z")
}
},
{ multi: true }
)
You will need to pass additional option argument {multi: true};
Look in to this https://education.10gen.com/courses/10gen/M101JS/2013_August/courseware/CRUD/Multi-update/

Related

Mongo DB aggregate match not returning value

I have the following mongo db schema and I am trying to build an aggregate query that searches under github_open_issues under the repo key and can return me a match for all the values with repoA as the value. I have tried the following as my query however its not returning any result. Im a bit confused why this is not working as I have another db with a schema similar to this and this type of query works there but here something seems to be different and is not working. I have also put together this interactive example mongoplayground
query
db.collection.aggregate([
{
"$unwind": "$github_open_issues"
},
{
"$match": {
"github_open_issues.repo": {
"$in": [
"repoA"
]
}
}
},
])
schema
[
{
"github_open_issues": {
"0": {
"git_url": "https://github.com/",
"git_assignees": "None",
"git_open_date": "2019-09-26",
"git_id": 253113,
"repo": "repoA",
"git_user": "userA",
"state": "open"
},
"1": {
"git_url": "https://github.com/",
"git_assignees": "None",
"git_open_date": "2019-11-15",
"git_id": 294398,
"repo": "repoB",
"git_user": "userB",
"state": "open"
},
"2": {
"git_url": "https://github.com/",
"git_assignees": "None",
"git_open_date": "2021-04-12",
"git_id": 661208,
"repo": "repoA",
"state": "open"
}
},
"unique_label_seen": {
"568": {
"label_name": "some label",
"times_seen": 12,
"535": {
"label_name": "another label",
"times_seen": 1
}
}
}
}
]
$objectToArray convert github_open_issues object to array in key-value format
$filter to iterate loop of above converted array and filter your search condition
$match to filter github_open_issues not empty
$arrayToObject convert github_open_issues array to object
db.collection.aggregate([
{
$addFields: {
github_open_issues: {
$filter: {
input: { $objectToArray: "$github_open_issues" },
cond: { $in: ["$$this.v.repo", ["repoA"]] }
}
}
}
},
{ $match: { github_open_issues: { $ne: [] } } },
{ $addFields: { github_open_issues: { $arrayToObject: "$github_open_issues" } } }
])
Playground
You query is correct but you data in schema placed wrong inside github_open_issues.repo your objects are place by numbers like {"0": {values... }, "1":{values... }} which cannot get your desired value. You can check the playground now playground

Trying to iterate through a collection of MongoDb results to update another collection

I'm trying to update some documents from DB-collection1 (source db) over to DB-collection2 (destination DB) .. all on the same MongoDb (with same permissions, etc).
So for each document from DB-Collection1, update a specific document in DB-collectoin2, if it exists.
The documents in DB-collection1 have following shape:
{
"_id": {
"commentId": "082f3de6-a268-46b5-803f-89bafd172621"
},
"appliesTo": {
"targets": [
{
"_id": {
"documentId": "b1eb1ad5-e74c-4a64-a4f3-bdc67ba70b35"
},
"type": "Document"
}
]
}
}
And the matching document in DB-collection2 is:
{
"_id": {
"documentId": "b1eb1ad5-e74c-4a64-a4f3-bdc67ba70b35"
},
"name": "jill"
},
I'm using a cursor to iterate through the source collection but I'm not sure how I can do this?
This is the javascript code mongo shell script I'm trying right now, when I run the following command on a machine where mongo is installed:
CLI: root#f0cc2f13e70c:/src/scripts# mongo --host localhost --username root --password example copyFoosToBars.js
// copyFoosToBars.js
function main() {
print('Starting script.')
print()
var foosDb = db.getSiblingDB('foos');
var barsDb = db.getSiblingDB('bars');
// Grab all the 'foos' which have a some barId in some convoluted schema.
var sourceFoos = foosDb.getCollection('foos')
.find(
{
"appliesTo.targets.type" : "Document",
"_meta.deleted": null
},
{
"_id" : 0,
"appliesTo.targets._id.documentId" : 1
}
);
sourceFoos.forEach(function(foo){
// Check if this document exists in the bars-db
var desinationBars = barsDb.getCollection('bars')
.find(
{
"_id.documentId" : foo.appliesTo.targets._id.documentId,
},
);
printjson(desinationBars);
// If destinationBars document exists, then add a the field 'Text' : 'hi there' to the document -or- update the existing field, if the 'Text' field already exists in this document.
});
print()
print()
print('----------------------------------------------')
}
main()
So here's some sample json output for the first part of the query -> which proves that I have some data which passes that 'find/search' clause:
Starting script.
{
"appliesTo" : {
"targets" : [
{
"_id" : {
"barId" : "810e66e2-66d1-44f4-be0e-980309d8df8f"
}
}
]
}
}
{
"appliesTo" : {
"targets" : [
{
"_id" : {
"barId" : "54f25223-67bb-4d5d-ad47-24392e4acbdf"
}
}
]
}
}
{
"appliesTo" : {
"targets" : [
{
"_id" : {
"barId" : "34c83da3-eafd-41bf-93af-3a45d1644225"
}
}
]
}
}
This doesn't work.
MongoDB server version: 4.0.22
WARNING: shell and server versions do not match
Starting script.
uncaught exception: TypeError: comment.appliesTo.targets._id is undefined :
main/
<snip>
Can someone please suggest some clues as to how I can fix this, please?
First of all you need to safeguard against multiple items in the appliesTo.targets.
A document
{
"_id": {
"commentId": "082f3de6-a268-46b5-803f-89bafd172621"
},
"appliesTo": {
"targets": [
{
"_id": {
"documentId": "should-not-be-updated"
},
"type": "AnyOtherType"
},
{
"_id": {
"documentId": "b1eb1ad5-e74c-4a64-a4f3-bdc67ba70b35"
},
"type": "Document"
}
]
}
}
Will be selected by
.find(
{
"appliesTo.targets.type" : "Document",
"_meta.deleted": null
},
{
"_id" : 0,
"appliesTo.targets._id.documentId" : 1
}
);
with the resulting document:
{
"appliesTo": {
"targets": [
{
"_id": {
"documentId": "should-not-be-updated"
}
},
{
"_id": {
"documentId": "b1eb1ad5-e74c-4a64-a4f3-bdc67ba70b35"
}
}
]
}
}
so foo.appliesTo.targets[0]._id.documentId will be "should-not-be-updated".
Structure of the document does not allow to use $elemMatch, so you have to either use aggregation framework or filter the array clientside. The aggregation has benefit of running serverside and reduce amount of data to transfer to the client.
Secondly, there is no point to find documents from DB-collection2. You can update all matching ones straight away, like in "update...where" SQL .
So the code must be something like following:
var sourceFoos = db.foos.aggregate([
{
$unwind: "$appliesTo.targets"
},
{
$match: {
"appliesTo.targets.type": "Document",
"appliesTo.targets._id.documentId": {
$exists: true
},
"_meta.deleted": null
}
},
{
$project: {
_id: 0,
"documentId": "$appliesTo.targets._id.documentId"
}
}
]);
sourceFoos.forEach(function(foo){
db.bars.updateMany(
{"_id.documentId" : foo.documentId},
{$set:{'Text' : 'hi there'}}
)
})
If there are a lot of documents expected in the cursor I would recommend to look at bulk updates to speed it up, but as I mentioned earlier in this case mongo shell might not be an ideal tool.
target is an array so for accessing to the _id try like this :
foo.appliesTo.targets[0]._id.barId
use async/await with try/catch and use .toArray() after find query
// copyFoosToBars.js
async function main() {
try {
var foosDb = db.getSiblingDB("foos");
var barsDb = db.getSiblingDB("bars");
// Grab all the 'foos' which have a some barId in some convoluted schema.
var sourceFoos = await foosDb
.getCollection("foos")
.find(
{
"appliesTo.targets.type": "Bar",
"_meta.deleted": null,
},
{
_id: 0,
"appliesTo.targets._id.fooId": 1,
}
).toArray();
for (foo of sourceFoos) {
var desinationBars = await barsDb
.getCollection("bars")
.find({
"_id.barId": foo.appliesTo.targets[0]._id.barId,
})
.toArray();
console.log(desinationBars);
if(desinationBars.length> 0){
//do somthings
}
}
} catch (error) {
console.log(error)
}
}
in the first find query you select "appliesTo.targets._id.fooId" : 1so select ( fooId but in the result json show "barId" : "810e66e2-66d1-44f4-be0e-980309d8df8f" it's conflict, Anyway, I hope this solution solves your problem

update query not working in mongoDB object

I am having below format document
{
"_id":"5f10481e74d83d4b726fdf33",
"name":"1234",
"settings":{
"allowedUser":true,
"theme":"lowergrade"
}
}
I want to write update query for settings.theme, so I have written below mongo query
db.test.update({
_id: "5f10481e74d83d4b726fdf33"
},
{
$set: {
"settings": {
"theme": "lowergrade123",
}
}
})
Update is happening proper but "allowedUser" : true, key and value was deleted.
This is my expected answer
{
"_id":"5f10481e74d83d4b726fdf33",
"name":"1234",
"settings":{
"allowedUser":true,
"theme":"lowergrade123"
}
}
db.test.update({ _id: "5f10481e74d83d4b726fdf33" }, {
$set: {
"settings.theme": "lowergrade123",
}
})
You need to use . to set a value of a field. You are actually replacing settings.

How to convert string field to object Id inside embedded document of already existing record?

I have a collection tblTesting with records saved like
{
"_id": ObjectId("5de9f044af647f21780056e1"),
"name": "abc",
"creditAccountDetails": {
"creditAccountNumber": "0200040671890190",
"creditAccountNumberId": "5db2efb5590a065abc006b12"
}
}
The embedded document "creditAccountDetails" has been wrongly saved. Now I am trying to update them by using mongodb command like
db.tblTesting.updateMany
{},
[
{ $set: { creditAccountDetails: [[ 'creditAccountNumberId' : ObjectId ($creditAccountNumber) ]] } },
{}
]
)
Basically I want that the command should be able to update all the records like
{
"_id": ObjectId("5de9f044af647f21780056e1"),
"name": "abc",
"creditAccountDetails":[ {
"creditAccountNumber": "0200040671890190",
"creditAccountNumberId": ObjectId("5db2efb5590a065abc006b12")
}
]
}
Please help!!!
Note that I am using mongo db 4.0
Since you are on mongodb version 4.0, which does not allow referring document fields in an update. A way to do this is iterate via cursor on the collection and update the field.
var cursor = db.collection.find({});
while (cursor.hasNext()) {
var doc = cursor.next();
db.collection.updateOne(
{
_id: doc._id
},
{
$set: {
creditAccountDetails: [
{
creditAccountNumber: doc.creditAccountDetails.creditAccountNumber,
creditAccountNumberId: ObjectId(
doc.creditAccountDetails.creditAccountNumberId
)
}
]
}
}
);
}
For readers who are on Mongodb 4.2.0+ which allows using aggregation pipeline ops in update methods updateOne, updateMany where document fields can be used as part of $set.
db.collection.updateMany({}, [
{
$set: {
creditAccountDetails: [
{
creditAccountNumber: "$creditAccountDetails.creditAccountNumber",
creditAccountNumberId: {
$toObjectId: "$creditAccountDetails.creditAccountNumberId"
}
}
]
}
}
]);
tblTesting's schema should have define type ObjectId for creditAccountNumberId
below example I used mongoose
const tblTesting = mongoose.Schema({
....
creditAccountNumberId: {
type: mongoose.Schema.Types.ObjectId,
},
....
}, {
collection: 'tblTesting',
timestamps: true,
strict: false,
})

Sum Embedded Document Fields in Mongo Aggregate Query

Data looks like:
{
"_id":5345345345,
"city": "Detroit",
"people":[
{
"name":"Larry",
"value":1200
},
{
"name":"Steve",
"value":1100
},
{
"name":"Larry",
"value":1000
},
{
"name":"Joe",
"value":800
},
{
"name":"Larry",
"value":500
},
{
"name":"Joe",
"value":700
}
]
}
I have this in the middle of an aggregate query. What can I do from here to sum the value field and have unique entry for each person in "people"? i.e.
{
"_id":5345345345,
"city": "Detroit",
"people":[
{
"name":"Larry",
"value":2700
},
{
"name":"Steve",
"value":1100
},
{
"name":"Joe",
"value":1500
}
]
}
Also, cannot have query that returns empty document in case the "people" array is empty, e.g., cannot do:
{$unwind:{
path:people
}
},
{$group :
{
_id:"$people.name",
total_value:{"$sum":"people.value"}
}
}
which would work fine, if it didn't return an empty document when people array is empty.
Need to use $addToSet and $sum somehow? How to do this keeping value associated with name?
Use
{
$unwind: {
path : "$people",
preserveNullAndEmptyArrays : true // this line!
}
}