Mongodb setting a value in an array without .$ - mongodb

I have the query
choices.update({"subchoices.id":'fh3284'},
{$set : { "subchoices.$.name" : "le_new_choice"}}
);
the document:
{ subchoices : [ { "id" : "fh3284", name : "choice1" },
{ "id" : "fh3286", name : "choice2" }
], ... }
Is it possible to rewrite this without using the .$. operator for the (on its own not the one in the $set,$query, etc)? Im using meteor and the client side implementation which doesn't take the .$. yet.

It seems that impossible to do such update withou .$.
I've spent a lot of time to try to sovle it, but couldn't.

Related

MongoEngine delete dict key from object field

I have a pretty complex Flask website with lots of DB interaction but have got stuck with a seemingly simple delete function!
I am trying to delete key 56 from sensordict in the (simplified) SiteConfig document below using MongoEngine
{
"_id" : "12345",
"sensordict" : {
"56" : {
"currentval" : 1.2,
"devicetype" : NumberInt(2)
},
"70" : {
"currentval" : 31.0,
"devicetype" : NumberInt(2)
}
},
"siteserial" : "45678",
"status" : NumberInt(1)
}
Code tried below where sensorid = '56':
def delete_sensor(siteconfig, sensorid):
dbsite = SiteConfig.objects(id=siteconfig.id).first()
dbsite.update(unset__sensordict__S=sensorid)
dbsite.save()
The code is failing at the update with mongoengine.errors.OperationError: Update failed (The positional operator did not find the match needed from the query. Unexpanded update: sensordict.$)
I suspect this is a simple one but have been way down the rabbit hole for a couple of hours on this one and any help would be much appreciated
Thanks
Bill
dbsite.update(unset__sensordict__S=sensorid) should be dbsite.update(unset__sensordict__56=1) since you have to unset the field with key '56'. This should work

Resolving MongoDB DBRef array using Mongo Native Query and working on the resolved documents

My MongoDB collection is made up of 2 main collections :
1) Maps
{
"_id" : ObjectId("542489232436657966204394"),
"fileName" : "importFile1.json",
"territories" : [
{
"$ref" : "territories",
"$id" : ObjectId("5424892224366579662042e9")
},
{
"$ref" : "territories",
"$id" : ObjectId("5424892224366579662042ea")
}
]
},
{
"_id" : ObjectId("542489262436657966204398"),
"fileName" : "importFile2.json",
"territories" : [
{
"$ref" : "territories",
"$id" : ObjectId("542489232436657966204395")
}
],
"uploadDate" : ISODate("2012-08-22T09:06:40.000Z")
}
2) Territories, which are referenced in "Map" objects :
{
"_id" : ObjectId("5424892224366579662042e9"),
"name" : "Afghanistan",
"area" : 653958
},
{
"_id" : ObjectId("5424892224366579662042ea"),
"name" : "Angola",
"area" : 1252651
},
{
"_id" : ObjectId("542489232436657966204395"),
"name" : "Unknown",
"area" : 0
}
My objective is to list every map with their cumulative area and number of territories. I am trying the following query :
db.maps.aggregate(
{'$unwind':'$territories'},
{'$group':{
'_id':'$fileName',
'numberOf': {'$sum': '$territories.name'},
'locatedArea':{'$sum':'$territories.area'}
}
})
However the results show 0 for each of these values :
{
"result" : [
{
"_id" : "importFile2.json",
"numberOf" : 0,
"locatedArea" : 0
},
{
"_id" : "importFile1.json",
"numberOf" : 0,
"locatedArea" : 0
}
],
"ok" : 1
}
I probably did something wrong when trying to access to the member variables of Territory (name and area), but I couldn't find an example of such a case in the Mongo doc. area is stored as an integer, and name as a string.
I probably did something wrong when trying to access to the member variables of Territory (name and area), but I couldn't find an example
of such a case in the Mongo doc. area is stored as an integer, and
name as a string.
Yes indeed, the field "territories" has an array of database references and not the actual documents. DBRefs are objects that contain information with which we can locate the actual documents.
In the above example, you can clearly see this, fire the below mongo query:
db.maps.find({"_id":ObjectId("542489232436657966204394")}).forEach(function(do
c){print(doc.territories[0]);})
it will print the DBRef object rather than the document itself:
o/p: DBRef("territories", ObjectId("5424892224366579662042e9"))
so, '$sum': '$territories.name','$sum': '$territories.area' would show you '0' since there are no fields such as name or area.
So you need to resolve this reference to a document before doing something like $territories.name
To achieve what you want, you can make use of the map() function, since aggregation nor Map-reduce support sub queries, and you already have a self-contained map document, with references to its territories.
Steps to achieve:
a) get each map
b) resolve the `DBRef`.
c) calculate the total area, and the number of territories.
d) make and return the desired structure.
Mongo shell script:
db.maps.find().map(function(doc) {
var territory_refs = doc.territories.map(function(terr_ref) {
refName = terr_ref.$ref;
return terr_ref.$id;
});
var areaSum = 0;
db.refName.find({
"_id" : {
$in : territory_refs
}
}).forEach(function(i) {
areaSum += i.area;
});
return {
"id" : doc.fileName,
"noOfTerritories" : territory_refs.length,
"areaSum" : areaSum
};
})
o/p:
[
{
"id" : "importFile1.json",
"noOfTerritories" : 2,
"areaSum" : 1906609
},
{
"id" : "importFile2.json",
"noOfTerritories" : 1,
"areaSum" : 0
}
]
Map-Reduce functions should not be and cannot be used to resolve DBRefs in the server side.
See what the documentation has to say:
The map function should not access the database for any reason.
The map function should be pure, or have no impact outside of the
function (i.e. side effects.)
The reduce function should not access the database, even to perform
read operations. The reduce function should not affect the outside
system.
Moreover, a reduce function even if used(which can never work anyway) will never be called for your problem, since a group w.r.t "fileName" or "ObjectId" would always have only one document, in your dataset.
MongoDB will not call the reduce function for a key that has only a
single value

Updating MongoDB object field

I have searched and tried many answers but I can't seem to get this working. I have a user in MongoDB (db.users...) and I'm trying to update a field titled 'role' in one particular document. If I perform a :
db.users.find();
I get:
{ "_id" : "fDx2g34G8vxsDu3vf", "createdAt" : ISODate("2014-06-03T16:31:47.382Z"),
"emails" : [ { "address" : "andrewmlarking#gmail.com", "verified" : false } ],
"profile" : { "name" : "Admin", "role" : "user", "division" : "0", "enrolled" : "false" },
"services" : { "password" : { "srp" : { "identity" "dEad06c_5mjzsprUzgRyh6tB66OiaybdLzbnxFzO1xh",
"salt" : "zqWsh etc etc
Which is fine, if I then perform a:
db.users.update({_id:"2xnoy3jqcHCaFp7Br"}, { role:"admin"});
I get a:
assert failed : need an object Error: Printing Stack Trace
at printStackTrace (src/mongo/shell/utils.js:37:15)
at doassert (src/mongo/shell/assert.js:6:5)
at assert (src/mongo/shell/assert.js:14:5)
at DBCollection.update (src/mongo/shell/collection.js:220:5)
at (shell):1:10
Any ideas?
Thanks.
First of all if you are trying to update the role field of the profile subdocument your syntax is off. It should be:
db.users.update({_id:"2xnoy3jqcHCaFp7Br"}, {$set: { "profile.role":"admin"}})
Otherwise you will just delete all the other fields of the document.
Regarding the error message, that normally only occurs when you run update with a single argument (the query) without the fields to update. Are you sure you are calling it as documented in your question?
Regardless I would highly advise you to review MongoDB documentation for updates:
http://docs.mongodb.org/manual/tutorial/modify-documents/
Maybe you should try adding ObjectId to the query.
Also, as pointed by #John Petrone you should use $set
I usually do
db.users.update({_id:ObjectId("2xnoy3jqcHCaFp7Br")}, {$set: { "profile.role":"admin"});

mongo db update subdocument - subdocument not in array

I have a document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303
}
},
"_id" : "pl"
}
I am trying to update this document like this,
{
"S" : {
"500209" : {
"total_income" : 38982,
"interest_income" : 1714,
"reported_eps" : 158.76,
"year" : 201303,
"yield": 1001, <== inserted a new attribute
}
},
"_id" : "pl"
}
I have tried this,
db.my_collection.update({_id: 'pl'},{$set: {'S.500209.yield': 1}})
But I couldn't make it. And I searched in stack overflow and google but I couldn't find out.
I got so many answers but most of them keeping sub-docuemnts in array.
Pleas help me to solve my issue, and please tell me why most of them keeping subdocuments in array.
Number key might cause the problem. Update your field name.
db.my_collection.update({_id: 'pl'},{$set: {'S.a500209.yield': 1}})
EDIT
Upgrade mongo version. It works fine with 2.4.

MongoDB query with multiple search terms(regexes) in c# with 10gens driver?

If we have
Blog{
Name 'Blog1'
Tags ['testing','visual-studio','2010','c#']
}
Blog{
Name 'Blog2'
Tags ['parallel','microsoft','c#']
}
Via the console we can execute and find all blog posts that contains some of the provided tags:
db.BlogPost.find({ 'Tags' : { '$regex' : ['/^Test/', '/^microsoft/', '/^visual/', '/^studio/', '/^c#/'] } });
How can we write the same query in c# 10gens driver ?
Is there any alternative if it can not be written via the 10gens c# driver ?
Query.Match only support one regex. Can we provide him multiple regexes, or we should combine
Query.Or(Query.Match("Test"), Query.Match("Micro"), Query.Match("Visual"))
I've managed to solve it with
I've managed to do it with
{ "$or" : [{ "Tags" : /^programm/i }, { "Tags" : /^microsoft/i }, { "Tags" : /^visual/i }, { "Tags" : /^studio/i }, { "Tags" : /^assert/i }, { "Tags" : /^2010/i }, { "Tags" : /^c#/i }] }
But something tells me that this is an ugly hack that may result in performance issues. What do you think guys ?
The final answer to the problem can be found on:
Official mongodb forum
Yes, the MongoDB c# driver should automatically do the right thing with instances of System.Text.RegularExpressions.Regex.
So you should be able to build the exact same query, except that you would use instances of Regex for ^Test, ^microsoft, ^visual, etc instead of strings.