Giter VIP home page Giter VIP logo

Comments (3)

tidwall avatar tidwall commented on May 22, 2024 1

The DescendRange signature is

DescendRange(index, lessOrEqual, greaterThan string, iterator func(key, value string) bool) error

So the line in your code:

tx.DescendRange("last_name", `{"name":{"last":""}}`, `{"name":{"last":"Ma"}}`, func(key, value string) bool {

should be

tx.DescendRange("last_name", `{"name":{"last":"Ma"}}`, `{"name":{"last":""}}`, func(key, value string) bool {

The buntdb.Desc function inverses the Ascend* and Descend* params and results.

For example:

db, _ := buntdb.Open(":memory:")
db.Update(func(tx *buntdb.Tx) error {
	tx.CreateIndex("last_name", "*", buntdb.Desc(buntdb.IndexJSON("name.last")))

	tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":"38"}`, nil)
	tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":"47"}`, nil)
	tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":"52"}`, nil)
	tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":"28"}`, nil)
	tx.Set("5", `{"name":{"first":"Jack","last":"Ma"},"age":"20"}`, nil)
	tx.Set("6", `{"name":{"first":"Blues","last":"Li"},"age":"31"}`, nil)
	tx.Set("7", `{"name":{"first":"Pony","last":"Ma"},"age":"3"}`, nil)
	tx.Set("8", `{"name":{"first":"Robin","last":"Li"},"age":"2"}`, nil)
	tx.Set("9", `{"name":{"first":"Larry","last":"Liu"},"age":""}`, nil)
	return nil
})

db.View(func(tx *buntdb.Tx) error {
	fmt.Println("---------------------")
	tx.Ascend("last_name", func(key, value string) bool {
		fmt.Printf("%s: %s\n", key, value)
		return true
	})

	fmt.Println("---------------------")
	tx.AscendRange("last_name", `{"name":{"last":"Ma"}}`, `{"name":{"last":""}}`, func(key, value string) bool {
		fmt.Printf("%s: %s\n", key, value)
		return true
	})
	return nil
})

returns

2: {"name":{"first":"Janet","last":"Prichard"},"age":"47"}
5: {"name":{"first":"Jack","last":"Ma"},"age":"20"}
7: {"name":{"first":"Pony","last":"Ma"},"age":"3"}
9: {"name":{"first":"Larry","last":"Liu"},"age":""}
6: {"name":{"first":"Blues","last":"Li"},"age":"31"}
8: {"name":{"first":"Robin","last":"Li"},"age":"2"}
1: {"name":{"first":"Tom","last":"Johnson"},"age":"38"}
4: {"name":{"first":"Alan","last":"Cooper"},"age":"28"}
3: {"name":{"first":"Carol","last":"Anderson"},"age":"52"}
---------------------
5: {"name":{"first":"Jack","last":"Ma"},"age":"20"}
7: {"name":{"first":"Pony","last":"Ma"},"age":"3"}
9: {"name":{"first":"Larry","last":"Liu"},"age":""}
6: {"name":{"first":"Blues","last":"Li"},"age":"31"}
8: {"name":{"first":"Robin","last":"Li"},"age":"2"}
1: {"name":{"first":"Tom","last":"Johnson"},"age":"38"}
4: {"name":{"first":"Alan","last":"Cooper"},"age":"28"}
3: {"name":{"first":"Carol","last":"Anderson"},"age":"52"}

from buntdb.

GreatMartial avatar GreatMartial commented on May 22, 2024

It turned out that because of carelessness, I read the wrong order of passing parameters.
this small problem still needs your help answer, i am very sorry.
Finally,thank you for your help.

from buntdb.

tidwall avatar tidwall commented on May 22, 2024

No worries. Iā€™m happy to help. :)

from buntdb.

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.