pymongo


Pymongo how to properly use $push to update an existing document


I am doing something wrong or I don't understand $push (probably don't understand). Anyway I have the following document:
{ "_id" : ObjectId("501c83051d41c8753e000000"),
"node" : "denver",
"enc" : { "environment" : "production", "classes" : { "denver" : "" } },
"inherit" : "default" }
And I am trying to make the document
{ "_id" : ObjectId("501c83051d41c8753e000000"),
"node" : "denver",
"enc" : { "environment" : "production",
"classes" : { "denver" : "", "boulder" : ""} },
"inherit" : "default" }
This is what my update statement looks like:
col.update({ 'node' : 'denver'},
{ '$push': { 'enc.classes' : {'boulder': ''}}},
True)
I don't get an error but the document never updates. If I change $push to $set then the denver is replaced with boulder.
Thanks for any assistance.
This query works.
db.foo.update({"node": "denver"}, {"$set": {"enc.classes.boulder": ""}}
$push does not work in this case because you are trying to use an array function on an object.
To use $push you would need to change your data structure to the following:
{
"_id" : ObjectId("501c83051d41c8753e000000"),
"node" : "denver",
"enc" : {
"environment" : "production",
"classes" : [
"denver"
]
},
"inherit" : "default"
}
Then your query would be:
col.update(
{
'node' : 'denver'
},
{
'$push': {
'enc.classes' : 'boulder'
}
},
True
)

Related Links

Spark Streaming: foreachRDD insert into mongoDB using python?
how to find right version of bson from pip for pymongo/mongoengine
How to enable `projection` in pymongo query class `find()`?
Variables don't hold value for very long pymongo ipython
pymongo upgrade to 3.0.3 causes this statement to fail
UUID to NUUID in Python
how to test for empty mongodb query in pymong
read video file from mongodb with pymongo
Get OperationFailure error with PyMongo 2.7.1
Inserting string as regular string in mongodb
Mongos + Pymongo 2.5 ==>No suitable hosts found
pymongo collection add item using update
How to convert objectid to string
Conditional atomic mongo remove with different results
python script to connect to mongodb master
flask-admin and pymongo search

Categories

HOME
php
xcode
ontouchlistener
subdomain
hid
dafny
consul
header
drag-and-drop
google-search-console
stored-procedures
msmq
reportviewer
rangy
jquery-file-upload
supervisord
robolectric
boomi
significance
pst
backtracking
acl
sipp
pdf.js
tracing
tf-idf
jackrabbit-oak
oracleforms
php-5.3
adapter
laravel-4.2
substance
progid
reporting
private-key
grails-2.5
orchardcms-1.8
code-rally
assemblies
python-sounddevice
right-click
grunt-contrib-watch
grails-spring-security
oauth2-playground
contactless-smartcard
httphandler
oracle-bmcs
nuclio
axis
.net-micro-framework
return-type
rm
knockout-2.0
phppgadmin
gestures
winston
stateless-session-bean
dynamics-crm-4
pjax
java-2d
tablespace
fuzzer
dmarc
declarative
onepage-checkout
textpattern
dsa
ambiguity
highest
mixins
wso2ml
modern.ie
ocmockito
subfolder
maven-shade-plugin
pass-by-value
mojo
uno
image-rotation
loginview
treeline
android-2.3-gingerbread
jini
csslint
wso2bam
assetic
pacman
struts2-json-plugin
workflow-manager-1.x
popup-blocker
n-queens
dynamic-c
servercontrol
motodev-studio
radscheduler
qt-mobility
botnet
photo-management
iphone-sdk-4.3

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App