问题描述
我想在mongodb集合中存在的字符串数组中插入一个字符串。 该集合看起来像:
{
"_id": some id,"urls": [url1,url2]
}
我想在对urls数组的每次插入调用中插入一个字符串 我该怎么办?
解决方法
import pydbus
adapter = pydbus.SessionBus().get('com.semanect.S8Displayd','/com/semanect/S8Displayd')
help(adapter.SetScreen)
运算符向后更改了一段时间,以允许将1到n个项的数组插入到现有数组中的任意位置:
$push
请注意,位置也可以为负。 -1是数组的末尾,-2是数组的末尾,等等。要在每个文档的db.foo.find();
{ "_id" : 0,"items" : [ "url1","url2" ] }
// Position starts at 0. Let's use position 1 for the demo:
db.foo.update({_id:0},{$push: {"items": {$each: ["foo","bar"],$position: 1}}});
db.foo.find();
{ "_id" : 0,"foo","bar","url2" ] }
数组中插入内容,请提供items
谓词和{}
选项:
multi:true
,
您的发布请求(例如,express.js)将如下所示:
const urlRouter = express.Router();
urlRouter.route('/your_route_here').post((req,res,next) => {
yourMongoCollectionSchema.create(req.body)
.then(url => {
console.log("Url added",url);
res.setHeader('Content-Type','application/json');
res.json(url);
},err => next(err))
.catch(err => next(err));
})