{
"_id" : 160,
"info" : [
{
'name': 'Serg',
'proff': 'hacker'
},
null,
]
}
As you can see I have null element in my array, I need a general solution that will remove null elements from info array.
正如您所看到的,我的数组中有null元素,我需要一个通用解决方案,它将从info数组中删除null元素。
I tried this:
我试过这个:
for doc in iter:
people.update({ '_id' : doc['_id']}, { '$pull' : { 'info' : 'null' }})
where iter
is a collection of documents. and people
is a collection
其中iter是一个文档集合。人是一个集合
I also tried this in the shell:
我也在shell中试过这个:
> db.people.findAndModify({ query: {}, update: {'$pull': {info:null} } } )
But none of the above examples delete this null from my documents!! ))
但上述示例都没有从我的文档中删除此null! ))
1 个解决方案
#1
18
This should work out for you. In python null is called None.
这应该适合你。在python中,null称为None。
for doc in iter:
people.update({'_id':doc[id]},{'$pull':{'info':None}})
Python中的null对象?
Also in mongo shell, this should work out:
同样在mongo shell中,这应该是有效的:
db.people.update({_id:160},{$pull:{info:null}})
If you want the update operator, to update more that one document at a time, that is to pull out null values from multiple documents, then you have to supply the multi:true option. Because by default, if the query arguemnt is left blank i.e. {} and mulit:true is not provided, update operator works on the first document that it finds
如果您想要更新运算符,要一次更新多个文档,即从多个文档中提取空值,则必须提供multi:true选项。因为默认情况下,如果查询arguemnt保留为空,即{}并且未提供mulit:true,则update运算符将对其找到的第一个文档进行处理
db.people.update({},{$pull:{info:null}},{multi:true})
#1
18
This should work out for you. In python null is called None.
这应该适合你。在python中,null称为None。
for doc in iter:
people.update({'_id':doc[id]},{'$pull':{'info':None}})
Python中的null对象?
Also in mongo shell, this should work out:
同样在mongo shell中,这应该是有效的:
db.people.update({_id:160},{$pull:{info:null}})
If you want the update operator, to update more that one document at a time, that is to pull out null values from multiple documents, then you have to supply the multi:true option. Because by default, if the query arguemnt is left blank i.e. {} and mulit:true is not provided, update operator works on the first document that it finds
如果您想要更新运算符,要一次更新多个文档,即从多个文档中提取空值,则必须提供multi:true选项。因为默认情况下,如果查询arguemnt保留为空,即{}并且未提供mulit:true,则update运算符将对其找到的第一个文档进行处理
db.people.update({},{$pull:{info:null}},{multi:true})