select function doesn't work just get the array I want


CallMeLoki :

I have a file like this

{
"_id": {
    "$oid": "570bc73da8ebd9005dd54de3"
},
"title": "dota",
"imgurl": "asd.com",
"description": "",
"hints": [
    {
        "date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
        "description": "narinin"
    },
    {
        "date": "2016-04-26 22:50:12.6069011 +0430 IRDT",
        "description": "doros shod"
    }
]
}

The script I execute is

hints := hints{}
err := db.C("games").Find(bson.M{"title": game}).Select(bson.M{"hints": 0}).One(&hints)

My two structures are

type Game struct {
Id          bson.ObjectId `bson:"_id,omitempty"`
Title       string        `json:"title"`
Imgurl      string        `json:"imgurl"`
Description string        `json:"desc"`
Hints       []*hint       `bson:"hints", json:"hints"`
}

type hint struct {
    Description string    `json:"desc"`
    Date        time.Time `json:"date"`
}

When I use the script, all I get is a date string that doesn't make sense, not even witch in the docs, how can I get part of the prompt from the game

novalagung:

You also have to keep using Gamestruct to receive results, even for hintscolumns only. Your select query should also be .Select(bson.M{"hints": 1}).

I fixed your code and tried this locally.

game := Game{}
err = db.C("games").Find(bson.M{"title": "dota"})
        .Select(bson.M{"hints": 1}).One(&game)
if err != nil {
    panic(err)
}

for _, hint := range game.Hints {
    fmt.Printf("%#v\n", *hint)
}

All properties except gameare empty Hints.

Edit 1

To get the first 10 rows hints, the easiest way is to play the slice, but that's bad because it needs to get all the rows first.

for _, hint := range game.Hints[:10] { // 10 rows
    fmt.Printf("%#v\n", *hint)
}

Another solution (better) is by using $sliceon the .Select()query.

selector := bson.M{"hints": bson.M{"$slice": 10}} // 10 rows

err = db.C("so").Find(bson.M{"title": "dota"})
        .Select(selector).One(&game)

Edit 2

Use []int{skip, limit}on $sliceto support skip and limit.

selector := bson.M{"hints": bson.M{"$slice": []int{0, 10}}}

Related


select function doesn't work just get the array I want

CallMeLoki : I have a file like this { "_id": { "$oid": "570bc73da8ebd9005dd54de3" }, "title": "dota", "imgurl": "asd.com", "description": "", "hints": [ { "date": "2016-04-26 22:50:12.6069011 +0430 IRDT", "description": "narinin" }

select function doesn't work just get the array I want

CallMeLoki : I have a file like this { "_id": { "$oid": "570bc73da8ebd9005dd54de3" }, "title": "dota", "imgurl": "asd.com", "description": "", "hints": [ { "date": "2016-04-26 22:50:12.6069011 +0430 IRDT", "description": "narinin" }

select function doesn't work just get the array I want

CallMeLoki : I have a file like this { "_id": { "$oid": "570bc73da8ebd9005dd54de3" }, "title": "dota", "imgurl": "asd.com", "description": "", "hints": [ { "date": "2016-04-26 22:50:12.6069011 +0430 IRDT", "description": "narinin" }

I just want to require and but it doesn't work

Kirihadino Moyer The problem is: Write Python code for a program that adds all multiples of 7 and 9 to 600 (including 600) Here is what I did: summ = 0 for i in range(1, 601): if i % 7 == 0 and i % 9 == 0: summ+=i print(summ) output: 63 18

Sorting an array of structs doesn't work as I want it to work

Alexander Vistowski I'm having trouble sorting a struct. I have a car structure with make ('Marke'), model ('Modelis') and price ('Kaina') information. char brandWhen the user enters that make, model and price, the program should sort these arrays by price fro

Sorting an array of structs doesn't work as I want it to work

Alexander Vistowski I'm having trouble sorting a struct. I have a car structure with make ('Marke'), model ('Modelis') and price ('Kaina') information. char brandWhen the user enters that make, model, and price, the program should sort these arrays by price fr

Sorting an array of structs doesn't work as I want it to work

Alexander Vistowski I'm having trouble sorting a struct. I have a car structure with make ('Marke'), model ('Modelis') and price ('Kaina') information. char brandWhen the user enters that make, model and price, the program should sort these arrays by price fro

Sorting an array of structs doesn't work as I want it to work

Alexander Vistowski I'm having trouble sorting a struct. I have a car structure with make ('Marke'), model ('Modelis') and price ('Kaina') information. char brandWhen the user enters that make, model, and price, the program should sort these arrays by price fr

Sorting an array of structs doesn't work as I want it to work

Alexander Vistowski I'm having trouble sorting a struct. I have a car structure with make ('Marke'), model ('Modelis') and price ('Kaina') information. char brandWhen the user enters that make, model, and price, the program should sort these arrays by price fr

I want to reset x through a function but it doesn't work

el liucar Why didn't this function reset x? My code looks like this: def reset(): x=0 x = 22 reset() print(x) expected result x = 0, actual resultx = 22 soothsayer You need to know more about scopes in Python. Assignments to names always go into the inne

I want to reset x through a function but it doesn't work

el liucar Why didn't this function reset x? My code looks like this: def reset(): x=0 x = 22 reset() print(x) expected result x = 0, actual resultx = 22 soothsayer You need to know more about scopes in Python. Assignments to names always go into the inne

random.randint function doesn't work the way I want

username Whenever I run the program (not a serious one, just messing with the concept of things) the damage it does monster (monster[1])repeats the same numbers over and over. It randomizes once and then repeats. I understand what's going on, but I don't under

tkinter unbind function doesn't work, i want

Zeng Fanzhi I have some code here: def __GameOver__(self): self.canvas.unbind('<Up>'); #other function --- time.sleep(2) self.canvas.bind('<Up>', func); self.root.after(40,self.GameMainLoop) pass What I want is that when the game

I just want to get random string in array

user I just want to get random strings in an array. So far I have: import java.util.Random; public class FootyDraw { public static void main(String[] args) { Random r = new Random(); String[] teams = {"Arsenal", "

I just want to get random string in array

user I just want to get random strings in an array. So far I have: import java.util.Random; public class FootyDraw { public static void main(String[] args) { Random r = new Random(); String[] teams = {"Arsenal", "

I just want to get random string in array

user I just want to get random strings in an array. So far I have: import java.util.Random; public class FootyDraw { public static void main(String[] args) { Random r = new Random(); String[] teams = {"Arsenal", "

I just want to get random string in array

user I just want to get random strings in an array. So far I have: import java.util.Random; public class FootyDraw { public static void main(String[] args) { Random r = new Random(); String[] teams = {"Arsenal", "

CoordinatorLayout doesn't work like I want

weapon jeans I have this layout code in my activity. In ViewPagerI have Fragment which contains RecyclerView. When I scroll to the top of the list, I have to scroll it again to expand the CoordinatorLayout. How to do this in one seamless scroll: <android.suppo

Why doesn't this macro work as I want it to?

Ben I'm still a bit new to C, so bear with me. I'm trying to be able to reference elements of a struct by index. I thought macros would do the trick, but apparently not. Can anyone explain why the following doesn't work? #include <stdio.h> #define E(Structure

Why doesn't this macro work as I want it to?

Ben I'm still a bit new to C, so bear with me. I'm trying to be able to reference elements of a struct by index. I thought macros would do the trick, but apparently not. Can anyone explain why the following doesn't work? #include <stdio.h> #define E(Structure

into denmark time - it doesn't work as I want it to

Jasper Peterson I tried to match my time to Danish time, but it didn't work very well. <?php echo date("d/m/Y - H:s", $online_sidste);?> I just want it to look like this: 15/10-2013-08:01 I've done her work: date("d/m-Y - H:i", strtotime($online_sidste)); gl

Postgresql: "or" doesn't work as I want

Jupiter I have a problem with my request: This request works: SELECT o.nom, a.url, a.apikey FROM Organisation o, API a, Acheter ac WHERE o.id_organisation = a.id_organisation AND ac.id_organisation = o.id_organisation AND ac.id_device = :idd ; This request

The code doesn't work for Scroll as I want

Shan Khaliq I'm using jquery to get the event scroll, when the window scrolls, that scroll will give some value to the div, the code can tell me if it's wrong and how to make it work $(window).scroll(function (){ var scrPos = $(this).scrollTop(); $('feed

pushd with rd doesn't work as I want

user1540911 I've tried like this pushd \\somedrive.se\Install\Paket\XXX\ echo "start..." for /f %i in ('dir /a:d /b bu* ') do echo /q %i popd and got this output when running from a script: Z:\Paket\XXX>echo "start..." "start..." /b was unexpected at this tim