Giter VIP home page Giter VIP logo

Comments (4)

jraede avatar jraede commented on September 14, 2024

from bongo.

wxf4150 avatar wxf4150 commented on September 14, 2024
var parentColl *bongo.Collection
var childColl *bongo.Collection
type parent struct{
	bongo.DocumentBase `bson:",inline"`
	Childs []*Child
}
type Child struct{
	bongo.DocumentBase `bson:",inline"`
	ParentID bson.ObjectId
	Status int
	diffTracker *bongo.DiffTracker
}

func Test_Bongo(t *testing.T){
	parentColl=utils.Bconn.Collection("parent")
	childColl=utils.Bconn.Collection("child")
	parent:=new(parent)
	parentColl.Save(parent)
	child:=new(Child)
	child.ParentID=parent.Id
	childColl.Save(child)

	for i:=1;i<100;i++{
		child.Status=i
		childColl.Save(child) //will invoke CascadeSave  using (goroutine CascadeSave)
	//code: https://github.com/go-bongo/bongo/blob/master/collection.go#L147
	}

	//**the end**: len(parent.Childs)!=1 or parent.Childs[0].Status!=99
}
func (c *Child) GetCascade(collection *bongo.Collection) []*bongo.CascadeConfig {
	connection := collection.Connection
	rel :=c
	cascadeMulti := &bongo.CascadeConfig{
		Collection:  connection.Collection("parent"),
		//Properties:  []string{"name","orgname","status"},
		Data:rel,
		ThroughProp: "childs",
		RelType:     bongo.REL_MANY,
		Query: bson.M{
			"_id": c.ParentID,
		},
	}
	if c.diffTracker!=nil && c.diffTracker.Modified("ParentID") {
		origId, _ := c.diffTracker.GetOriginalValue("ParentID")
		if origId != nil {
			oldQuery := bson.M{
				"_id": origId,
			}
			cascadeMulti.OldQuery = oldQuery
		}

	}
	return []*bongo.CascadeConfig{ cascadeMulti}
}

the end:
parent doccument error,
len(parent.Childs)!=1 or parent.Childs[0].Status!=99

from bongo.

wxf4150 avatar wxf4150 commented on September 14, 2024

or update this way

	for i:=1;i<20;i++{
		cloneChild:=*child
		cloneChild.Status=i
		childColl.Save(&cloneChild) 
	}
	time.Sleep(2*time.Second)

the end:
parent doccument error,
len(parent.Childs)!=1 or parent.Childs[0].Status!=19

from bongo.

jraede avatar jraede commented on September 14, 2024

I could see this being a race condition if there are multiple users updating the same record. Solution seems to be just removing the go in front of the CascadeSave here https://github.com/go-bongo/bongo/blob/master/collection.go#L147

Can you submit a PR?

from bongo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.