Updating MongoDB object field - mongodb

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"});

Related

Query MongoDB collection

I have a MongoDB collection like this:
{
"_id" : {
"processUuid" : "653d0937-2afc-4915-ad42-d2b69f344402",
"partnerId" : "p377"
},
"isComplete" : true,
"tasks" : {
"dbb361a7-4b73-4691-bde5-2b160346464f" : {
"sku" : "4060079000048",
"status" : "FAILED",
"errorList" : [....]
},
"790dbc6f-563d-4eb7-931c-3cc604563dc1" : {
"sku" : "4060079000130",
"status" : "SUCCESSFUL",
"errorList" : [....]
},
... more tasks
}
... more processes
}
I want to query a certain sku and couldnt find a way. I know i could write an aggregation pipeline to project the inner part of a task, but then i would lose the task identifier which i need in order to add some stuff to a task.
I know the document structure is weird, i would have done it different, but its given.
I tried what i found about querying nested documents and so on, but unfortunately didnt get it. Any hints are appreciated.

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

Filtering Mongo items by multiple fields and subfields

I have the following items in my collection:
> db.test.find().pretty()
{ "_id" : ObjectId("532c471a90bc7707609a3d4f"), "name" : "Alice" }
{
"_id" : ObjectId("532c472490bc7707609a3d50"),
"name" : "Bob",
"partner_type1" : {
"status" : "rejected"
}
}
{
"_id" : ObjectId("532c473e90bc7707609a3d51"),
"name" : "Carol",
"partner_type2" : {
"status" : "accepted"
}
}
{
"_id" : ObjectId("532c475790bc7707609a3d52"),
"name" : "Dave",
"partner_type1" : {
"status" : "pending"
}
}
There are two partner types: partner_type1 and partner_type2. A user cannot be accepted partner in the both of types. But he can be a rejected partner in partner_type1 but accepted in the another, for example.
How can I build Mongo query that fetches the users that can become partners?
When your user can only be accepted in one partner-type, you should turn it around: Have a field accepted_as:"partner_type1" or accepted_as:"partner_type2". For people who aren't accepted yet, either have no such field or set it to null.
In both cases, your query to get any non-accepted will then be:
{
data.accepted_as: null
}
(null matches both non-existing fields as well as fields explicitly set to null)
For me the logical schema would be this:
"partner : {
"type": 1,
"status" : "rejected"
}
At least that keeps the paths consistent between documents.
So if you want to stay away from using mapReduce type methods to find out "which field" it is on, and otherwise use plain queries and the aggregation pipeline, then don't vary field paths on documents. If you alter the "data" then that is the most consistent form.

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.

Retrieving only the relevant part of a stored document

I'm a newbie with MongoDB, and am trying to store user activity performed on a site. My data is currently structured as:
{ "_id" : ObjectId("4decfb0fc7c6ff7ff77d615e"),
"activity" : [
{
"action" : "added",
"item_name" : "iPhone",
"item_id" : 6140,
},
{
"action" : "added",
"item_name" : "iPad",
"item_id" : 7220,
}
],
"name" : "Smith,
"user_id" : 2
}
If I want to retrieve, for example, all the activity concerning item_id 7220, I would use a query like:
db.find( { "activity.item_id" : 7220 } );
However, this seems to return the entire document, including the record for item 6140.
Can anyone suggest how this might be done correctly? I'm not sure if it's a problem with my query, or with the structure of the data itself.
Many thanks.
You have to wait the following dev: https://jira.mongodb.org/browse/SERVER-828
You can use $slice only if you know insertion order and position of your element.
Standard queries on MongoDb always return all document.
(question also available here: MongoDB query to return only embedded document)