小编fel*_*lix的帖子

Mongodb从String到ObjectId加入_id字段

我有两个系列

  1. 用户

             {
                 "_id" : ObjectId("584aac38686860d502929b8b"),
                 "name" : "John"
             }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 角色

     {
         "_id" : ObjectId("584aaca6686860d502929b8d"),
         "role" : "Admin",
         "userId" : "584aac38686860d502929b8b"  
     }
    
    Run Code Online (Sandbox Code Playgroud)

我想基于userId(在角色集合中) - _id(在用户集合中)加入这些集合.

我尝试了以下查询:

db.role.aggregate(
{
   $lookup:
   {
       from: 'user',
       localField: 'userId',
       foreignField: '_id',
       as: 'output'
   }
}
);
Run Code Online (Sandbox Code Playgroud)

只要我将userId存储为ObjectId,这就给了我预期的结果.当我的userId是一个字符串时,没有结果.Ps:我试过了

foreignField:'_ id'.valueOf()

foreignField:'_ id'.toString()

.但是基于ObjectId-string字段匹配/加入没有运气.

任何帮助将不胜感激.

lookup join mongodb aggregation-framework objectid

17
推荐指数
3
解决办法
2万
查看次数

如果在一个中发生错误,请关闭多个goroutine

考虑这个功能:

func doAllWork() error {
    var wg sync.WaitGroup
    wg.Add(3)
    for i := 0; i < 2; i++ {
        go func() {
            defer wg.Done()
            for j := 0; j < 10; j++ {
                result, err := work(j)
                if err != nil {
                    // can't use `return err` here
                    // what sould I put instead ? 
                    os.Exit(0)
                }
            }
        }()
    }
    wg.Wait()
    return nil
}
Run Code Online (Sandbox Code Playgroud)

在每个goroutine中,该函数work()被调用10次.如果一个调用work()在任何正在运行的goroutine中返回错误,我希望所有goroutine立即停止,并退出程序.可以os.Exit()在这里使用吗?我该怎么处理?


编辑:这个问题不同于如何停止goroutine,因为我需要关闭所有goroutine如果一个错误发生

error-handling synchronization exit go goroutine

11
推荐指数
2
解决办法
4303
查看次数

将 requirejs 和普通 js 文件合并在一起

我正在开发一个小型网站,主要的 HTML 页面基本上如下所示:

<html lang="en">
  <head>
    <script src="ace.js" type="text/javascript"><script>
    <script src="ext-language_tools.js" type="textjavascript"></script>
    <script src="mode-mongo.js" type="text/javascript"></script>
    <script src="playground.js" type="text/javascript"></script>
    <script type="text/javascript">
    
      window.onload = function() {
    
        ace.config.setModuleUrl("ace/mode/mongo", "mode-mongo.js")
    
        configEditor = ace.edit(document.getElementById("editor"), {
            "mode": "ace/mode/mongo",
            "useWorker": false
        })
      }
    </script>
  </head>
  <body>
    <div class="editor">
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

(真实的可以在这里看到)。

以下是未缩小的文件:

前3个js文件使用requirejs,第4个只是普通js

有没有办法将这 4 个文件合并到一个文件中,以便在我的 HTML 中包含类似的内容?

<html lang="en">
  <head>
    <script src="bundle.js" type="text/javascript"><script>
    <script type="text/javascript">
    
      window.onload = function() {

        configEditor = ace.edit(document.getElementById("editor"), {
            "mode": "ace/mode/mongo",
            "useWorker": false
        }) …
Run Code Online (Sandbox Code Playgroud)

javascript merge bundle module requirejs

11
推荐指数
1
解决办法
979
查看次数

Mongodb递归查询

我的taxon集合中有以下架构:

{ 
  "_id": 1, 
  "na": [ "root_1",
        "root_2",
        "root_3" ], 
  "pa": 1 
},{
  "_id": 2, 
  "na": [ "name_1", 
        "name_2", 
        "name_3"], 
  "pa": 1
},{
  "_id": 4, 
  "na": [ "otherName_1", 
        "otherName_2", 
        "otherName_3"],
  "pa": 2
}
Run Code Online (Sandbox Code Playgroud)

每个文档都由父字段与另一个文档相关联,该字段对应于_id其父项.

我想执行递归搜索以获得以下结果:

{ "_id": 4, 
  "nameList": [ "otherName_1",
              "name_1",
              "root_1"]
} 
Run Code Online (Sandbox Code Playgroud)

从某个文件中_id获取na每个父项的数组的第一项,直到_id: 1达到 文档为止

我目前通过执行X查询得到这个结果(一个通过父文档,例如这里有3个),但我很确定这可以使用单个查询来实现.我已经看过新的$ graphLookup操作符了,但无法用它来解决问题...

是否可以使用MongoDB 3.4.1在单个查询中实现此目的?

编辑

我每次都会运行50个文档,因此最佳解决方案是将所有内容组合在一个查询中

例如,它看起来像

var listId = [ 4, 128, 553, 2728, ...];
var cursor = db.taxon.aggregate([
  {$match: …
Run Code Online (Sandbox Code Playgroud)

recursion mongodb aggregation-framework

9
推荐指数
1
解决办法
4981
查看次数

产生随机布尔

生成随机布尔的最快方法是什么?

目前我正在这样做:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

// random generator
var src = rand.NewSource(time.Now().UnixNano())
var r = rand.New(src)

func main() {
    for i := 0; i < 100; i++ {
        // generate a random boolean and print it 
        fmt.Printf("bool: %s\n", r.Intn(2) != 0)
    }
}
Run Code Online (Sandbox Code Playgroud)

我该如何改善呢?

random boolean go

5
推荐指数
2
解决办法
4055
查看次数

在go中的给定范围内生成随机的128位十进制

假设我们有一个随机数生成器,可以生成随机的 32 位或 64 位整数(如标准库中的rand.Rand

在给定范围内生成随机 int64[a,b]相当容易:

rand.Seed(time.Now().UnixNano())
n := rand.Int63n(b-a) + a
Run Code Online (Sandbox Code Playgroud)

是否可以从 32 位或 64 位随机整数的组合在给定范围内生成随机的 128 位十进制(如规范 IEEE 754-2008 中所定义)?

random go

5
推荐指数
1
解决办法
768
查看次数

将char数组转换为多个String

我需要将char数组转换为一定大小的几行.例如,考虑这个数组:

char[] bases = new char[]{'a', 'c', 'c', 't', 'a', 'c', 'a', 't', 'a', 'c', 'c', 't', 'a', 'c', 'a', 't'};
Run Code Online (Sandbox Code Playgroud)

预期的输出大小为5:

accta
catac
ctaca
Run Code Online (Sandbox Code Playgroud)

我目前正在使用此代码:

    int ls = 5; 
    String str = ""; 
    for (int i = 1; i < bases.length; i++) {
        str += bases[i - 1];
        if (i%ls == 0) {
            str += '\n';
        }
    }
Run Code Online (Sandbox Code Playgroud)

在java 8中是否有一些内置函数来实现这一点?有没有更好的方法来解决这个问题?

java arrays string java-8

3
推荐指数
1
解决办法
349
查看次数

go中的map vs switch性能

考虑这个基准,我们比较地图访问与交换机

var code = []int32{0, 10, 100, 100, 0, 10, 0, 10, 100, 14, 1000, 100, 1000, 0, 0, 10, 100, 1000, 10, 0, 1000, 12}
var mapCode = map[int32]int32{
    0:    1,
    10:   2,
    100:  3,
    1000: 4,
}

func BenchmarkMap(b *testing.B) {
    success := int32(0)
    fail := int32(0)
    for n := 0; n < b.N; n++ {
        // for each value in code array, do a specific action
        for _, v := range code {
            c, ok := mapCode[v] …
Run Code Online (Sandbox Code Playgroud)

performance hashmap go switch-statement

3
推荐指数
2
解决办法
1665
查看次数

具有多种返回类型的接口方法

我正在为接口而苦苦挣扎。考虑一下:

type Generatorer interface {
    getValue() // which type should I put here ? 
}

type StringGenerator struct {
    length         int
}

type IntGenerator struct {
    min            int
    max            int
}

func (g StringGenerator) getValue() string {
    return "randomString"
}

func (g IntGenerator) getValue() int {
    return 1
}
Run Code Online (Sandbox Code Playgroud)

我希望getValue()函数返回 astring或 an int,具体取决于它是从StringGeneratorIntGenerator

当我尝试编译它时,出现以下错误:

不能将 s(类型 *StringGenerator)用作数组或切片文字中的类型 Generatorer:*StringGenerator 未实现 Generatorer(getValue 方法的类型错误)

有 getValue() 字符串
想要 getValue()

我怎样才能做到这一点?

interface object go

2
推荐指数
1
解决办法
2614
查看次数

uniq 命令未检测到重复行

我在使用 unix 命令时遇到了困难uniq。我有一个包含 ids 列表的文件,如下所示(输出head -5 list.txt):

IBNUKWG02JZU4E
IBNUKWG02JZULO
IBNUKWG02JZUMG
IBNUKWG02JZUZS
IBNUKWG02JZV0R
Run Code Online (Sandbox Code Playgroud)

这些文件包含619142行 ( cat list.txt | wc -l),并且包含重复项,例如,如果我运行命令(-c标志返回该行出现的次数)

cat list.txt | grep IBNUKWG02JZULO | uniq -c 
Run Code Online (Sandbox Code Playgroud)

它返回

  2 IBNUKWG02JZULO
Run Code Online (Sandbox Code Playgroud)

但如果我运行命令(-u标记为仅打印唯一行)

   cat list.txt | uniq -u | wc -l 
Run Code Online (Sandbox Code Playgroud)

它返回619142,就好像没有检测到重复行一样。这怎么可能 ?

unix linux bash shell uniq

0
推荐指数
1
解决办法
1662
查看次数

返回接口{}而不是int64时的额外分配

我有一个生成随机的函数,int64并返回interface{}如下:

func Val1(rnd rand.Source) interface{} {
 return rnd.Int63() 
} 
Run Code Online (Sandbox Code Playgroud)

现在考虑这个函数,它做同样的事情,但返回一个 int64

func Val2(rnd rand.Source) int64 {
 return rnd.Int63()
}
Run Code Online (Sandbox Code Playgroud)

我使用this(go test -bench=. -benchmem)对这两个函数进行了基准测试:

func BenchmarkVal1(b *testing.B) {
    var rnd = rand.NewSource(time.Now().UnixNano())
    for n := 0; n < b.N; n++ {
        Val1(rnd)
    }
}

func BenchmarkVal2(b *testing.B) {
    var rnd = rand.NewSource(time.Now().UnixNano())
    for n := 0; n < b.N; n++ {
        Val2(rnd)
    }
}
Run Code Online (Sandbox Code Playgroud)

得到了以下结果:

BenchmarkVal1-4    50000000         32.4 ns/op         8 B/op          1 allocs/op
BenchmarkVal2-4    200000000 …
Run Code Online (Sandbox Code Playgroud)

memory types allocation object go

0
推荐指数
1
解决办法
103
查看次数