Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 获取嵌套数组中具有特定ObjectId的数组对象_Arrays_Mongodb_Go_Aggregate_Mgo - Fatal编程技术网

Arrays 获取嵌套数组中具有特定ObjectId的数组对象

Arrays 获取嵌套数组中具有特定ObjectId的数组对象,arrays,mongodb,go,aggregate,mgo,Arrays,Mongodb,Go,Aggregate,Mgo,我试图根据对象的ObjectId获取一个特定的对象数组 这是我的MongoDB数据库: { "_id" : ObjectId("59edb571593904117884b721"), "userids" : [ ObjectId("59edb459593904117884b71f") ], "macaddress" : "MACADDRESS", "devices" : [ ], "projectorbrand" : "",

我试图根据对象的ObjectId获取一个特定的对象数组

这是我的MongoDB数据库:

{
    "_id" : ObjectId("59edb571593904117884b721"),
    "userids" : [
            ObjectId("59edb459593904117884b71f")
    ],
    "macaddress" : "MACADDRESS",
    "devices" : [ ],
    "projectorbrand" : "",
}
{
    "_id" : ObjectId("59edb584593904117884b722"),
    "userids" : [
            ObjectId("59edb459593904117884b71f"),
            ObjectId("59e4809159390431d44a9438")
    ],
    "macaddress" : "MACADDRESS2",
    "devices" : [ ],
    "projectorbrand" : "",
}
MongoDB中的命令是:

db.getCollection('co4b').find( {
    userids: { $all: [ ObjectId("59edb459593904117884b71f") ] }
} )
这将起作用,并将返回正确过滤的数组。 我想把这个问题翻译成Golang

这是我的密码:

pipe := bson.M{"userids": bson.M{"$all": objectId}}
var objects[]models.Objects
if err := uc.session.DB("API").C("objects").Pipe(pipe).All(&objects); err != nil {
    SendError(w, "error", 500, err.Error())
} else {
    for i := 0; i < len(objects); i++ {
        objects[i].Actions = nil
    }
    uj, _ := json.MarshalIndent(objects, "", " ")
    SendSuccessJson(w, uj)
}
pipe:=bson.M{“userid”:bson.M{“$all”:objectId}
var objects[]models.objects
如果错误:=uc.session.DB(“API”).C(“对象”).Pipe(Pipe).All(&objects);呃!=零{
SendError(w,“error”,500,err.error())
}否则{
对于i:=0;i
我得到了类似于
字段(管道)3的错误类型!=4
。我看到,
$all
需要字符串数组,但是如何通过ObjectId而不是字符串进行过滤呢


感谢您的帮助

您正试图在
mgo
解决方案中使用聚合框架,但是您尝试实现的查询没有使用聚合框架(也不需要)

查询:

db.getCollection('co4b').find({
    userids: {$all: [ObjectId("59edb459593904117884b71f")] }
})
可以简单地转换为
mgo
,如下所示:

c := uc.session.DB("API").C("objects")

var objects []models.Objects
err := c.Find(bson.M{"userids": bson.M{
    "$all": []interface{}{bson.ObjectIdHex("59edb459593904117884b71f")},
}}).All(&objects)
db.getCollection('co4b').find({
    userids: {$elemMatch: {$eq: ObjectId("59edb459593904117884b71f")}}
})
还请注意,如果将
$all
与单个元素一起使用,还可以使用
$elemMatch
实现该查询,这在MongoDB控制台中是这样的:

c := uc.session.DB("API").C("objects")

var objects []models.Objects
err := c.Find(bson.M{"userids": bson.M{
    "$all": []interface{}{bson.ObjectIdHex("59edb459593904117884b71f")},
}}).All(&objects)
db.getCollection('co4b').find({
    userids: {$elemMatch: {$eq: ObjectId("59edb459593904117884b71f")}}
})
在mgo中看起来是这样的:

err := c.Find(bson.M{"userids": bson.M{
    "$elemMatch": bson.M{"$eq": bson.ObjectIdHex("59edb459593904117884b71f")},
}}).All(&objects)

你什么时候收到错误?在运行go代码时还是在编译go代码时?在运行go代码时非常感谢您的明确回答!它与$elemMatch完美搭配!