gpt4 book ai didi

MongoServerError: Cannot do exclusion on field date in inclusion projection(MongoServerError:无法在包含预测中对字段日期执行排除)

转载 作者:bug小助手 更新时间:2023-10-25 16:53:24 32 4
gpt4 key购买 nike



I was working with the MongoDB Atlas Server...
and encountered this error...
What does it mean...?
Can someone explain in simple words plz...

我在使用MongoDB Atlas服务器...并遇到了这个错误。这是什么意思…?有没有人能用简单的话解释一下…


This was the query i was trying...

这就是我正在尝试的查询...


db.posts.find({}, {title: 1, date: 0})

The structure of the posts in database is as follows:

数据库中职位的结构如下:


[
{
_id: ObjectId("63739044de169f6d0h2e6a3d"),
title: 'Post 2',
body: 'a news post',
category: 'News',
likes: 1,
tags: [ 'news', 'events' ],
date: 'Tue Nov 15 2022 18:53:24 GMT+0530 (India Standard Time)'
},
{
_id: ObjectId("63739271de179f5d0e31e5b2"),
title: 'Post 1',
body: 'hey there, hemant here',
category: 'random',
likes: 1,
tags: [ 'random', 'events' ],
date: 'Tue Nov 15 2022 18:41:24 GMT+0530 (India Standard Time)'
}
]


But I got an error which says...

但我收到一个错误,上面写着..。


MongoServerError: Cannot do exclusion on field date in inclusion projection

I was trying to get all the document objects excluding the date parameter and including the title parameter but got an error...

我试图获取除日期参数和标题参数之外的所有文档对象,但得到一个错误...


更多回答

db.posts.find({}, {title: 1}) Just run this to get the title alone. Rest all fields will be excluded.

db.posts.find({},{title:1})只需运行此命令即可单独获取标题。其余所有字段将被排除。

优秀答案推荐

According to the documentation, first argument in find is filter and second is projection. projection allows you to specify fields to return._id is the only field which you need to explicitly exclude in the projection. For all other fields you just need to state the inclusion. You will have to follow the below format.

根据文献记载,FIND中的第一个参数是过滤器,第二个参数是投影。Projection允许您指定要返回的字段。_id是唯一需要在投影中显式排除的字段。对于所有其他字段,您只需说明包含内容。您必须遵循以下格式。


db.posts.find({}, {title: 1, body:1, category:1, likes:1, tags:1})


Another possibility is that

另一种可能性是,


Inside the 'posts' mongoose schema,
if we set the date key like this

在‘post’Mongoose模式中,如果我们这样设置日期键


date: {
type: string,
select: true
}

the select:true always returns the date key in all find queries

SELECT:TRUE始终在所有查找查询中返回日期键


and passing {date:0} in projection or dot_select(.select) object will return this error:

在投影或点选择(.select)对象中传递{date:0}将返回此错误:



Cannot do exclusion on field date in inclusion projection




This is a simple error to fix, and I will show you how. You need to understand how projection works.
But first, let's look at how the find() method works.
The find() method can take two parameters, which are 1. filter and 2. projection. There are two types of projection: 1. inclusion projection and 2. exclusion projection.

这是一个要修复的简单错误,我将向您展示如何修复。您需要了解投影是如何工作的。但首先,让我们看看find()方法是如何工作的。Find()方法可以接受两个参数,即1.Filter和2.Projection。投影有两种类型:1.包含投影和2.排除投影。



  1. Inclusion projection means specifying all fields to be included in result of querying a document. This is done by assigning 1 to all values of keys desired for inclusion in an object that is passed as the second parameter of the find method. Note that only the "_id" key can be added for exclusion when performing inclusion projection.


You can do this to perform inclusion projection for the title field on the post collection:

您可以这样做来对帖子集合上的标题字段执行包含投影:


db.posts.find({}, {"title": 1})

or


db.posts.find({}, {"title": 1, "_id": 0})

and the result will be:

其结果将是:


[
{
title: 'Post 1'
},
{
title: 'Post 2'
}
]


  1. Exclusion projection means specifying all fields to be excluded in result of querying a document. This is done by assigning 0 to all values of keys desired for excluded in an object that is passed as the second parameter of the find method.
    To achieve the same result as the inclusion projection above using the exclusion projection, you can do this:


db.posts.find({}, {"_id": 0, "body":0, "category":0, "likes":0, "tags":0, "date":0})

and the result will be:

其结果将是:


[
{
title: 'Post 1'
},
{
title: 'Post 2'
}
]

Thank you for reading. I hope this gives you a deep understanding of projection.

感谢您的阅读。我希望这能让你对投影有一个深刻的理解。



This is a lot late, but here's something that might help you to get all except the date:

这太晚了,但这里有一些东西可能会帮助你得到除了日期之外的所有东西:


db.posts.find({}, {date: 0})

更多回答

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com