如何在golang中为嵌套文档设置ObjectId?

时间:2016-12-29 07:26:42

标签: mongodb model-view-controller go

型号:

Id

当车辆myvehicle被推入func showAlertController() { //simple alert dialog let alertController = UIAlertController(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert); // Add Action alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)); //show it let btnImage = UIImage(named: "checkBoxImage")! let imageButton : UIButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) imageButton.setBackgroundImage(btnImage, for: UIControlState()) imageButton.addTarget(self, action: #selector(ViewController.checkBoxAction(_:)), for: .touchUpInside) alertController.view.addSubview(imageButton) self.present(alertController, animated: false, completion: { () -> Void in }) } func checkBoxAction(_ sender: UIButton) { if sender.isSelected { sender.isSelected = false let btnImage = UIImage(named: "checkBoxImage")! sender.setBackgroundImage(btnImage, for: UIControlState()) }else { sender.isSelected = true let btnImage = UIImage(named: "unCheckBoxImage")! sender.setBackgroundImage(btnImage, for: UIControlState()) } } 字段时,它的值为空。

如何为嵌套的Vehicle的id设置一个值?

1 个答案:

答案 0 :(得分:2)

对于未指定_id字段的文档(非子文档),会自动插入MongoDB ObjectId。这是为了唯一地标识文档。

在上面的例子中,如果要插入嵌套对象(子文档),MongoDB将不会自动插入Id字段。

但是,您可以为新推送的Vehicle文档创建ObjectId(唯一标识符)。例如:

new_object_id := bson.NewObjectId()

另见NewObjectId