如何在MongoDB中返回文档的ObjectId或_id?(How to return the ObjectId or _id of an document in MongoDB? and error “$in needs an array”)
我在MongoDB中有一个文档,我想得到这个文档的ObjectId,但到目前为止我还没有找到一个对我这样做的方法。
查询示例:
user= db.users.find({userName:"Andressa"})
这返回:
{ "_id" : ObjectId("53b1c579bdf3de74f76bdac9"), "userid" : 0, "userName" : "Andressa", "userEmail" : "dessa_beca@hotmail.com", "teams" : [ 1, 2, 3 ] }
我想让ObjectId做另一个查询。
例:
userID = `user._id();` //but this does not work, of course, its an example
所以,我可以使用ObjectId来执行另一个查询,如下所示:
userFind = db.users.find({_id: userID})
更新:此代码:
db.teams.find({_id:{$in: user.teams}})
返回此错误:
error: { "$err" : "Can't canonicalize query: BadValue $in needs an array", "code" : 17287
有人知道吗?
I have a document in MongoDB and I would like to get the ObjectId of this document, but I did not find so far a method that does this to me.
Example of query :
user= db.users.find({userName:"Andressa"})
This returns this :
{ "_id" : ObjectId("53b1c579bdf3de74f76bdac9"), "userid" : 0, "userName" : "Andressa", "userEmail" : "dessa_beca@hotmail.com", "teams" : [ 1, 2, 3 ] }
I want get the ObjectId to do another query .
Example:
userID = `user._id();` //but this does not work, of course, its an example
So, I could user the ObjectId to do another query like this:
userFind = db.users.find({_id: userID})
UPDATE: This code :
db.teams.find({_id:{$in: user.teams}})
returns this error:
error: { "$err" : "Can't canonicalize query: BadValue $in needs an array", "code" : 17287
Does someone know it?
原文:https://stackoverflow.com/questions/24557580
最满意答案
只需删除此:
//Make sure entity knows EquipmentOwned has been modified db.Entry(ninja.EquipmentOwned).State = EntityState.Modified;
这会导致错误。
EquipmentOwned
是List<Equipment>
。 它不是EF
Entry
,因此不会直接跟踪它。当您从此类集合中删除实体时,
EF
知道此特定忍者与此特定设备之间不存在任何关系。 它不会从数据库中删除设备,因为其他忍者可能会使用此设备。要完全删除它,您应该从相应的
DbSet<>
删除此设备,如下所示:using (var db = new NinjaContext()) { //db.Equipment is a DbSet<Equipment> //id is PrimaryKey of Equipment table var eq = db.Equipment.Find(id); db.Equipment.Remove(eq); db.SaveChanges(); }
Just remove this:
//Make sure entity knows EquipmentOwned has been modified db.Entry(ninja.EquipmentOwned).State = EntityState.Modified;
This causes the error.
EquipmentOwned
is aList<Equipment>
. It is not anEntry
inEF
terms so it is not tracked directly by it.When you delete an entity from such collection,
EF
knows that there won't be any relationship between this particular ninja and this particular equipment. It won't delete equipment from database because other ninjas may use this equipment.To delete it completely you should remove this equipment from corresponding
DbSet<>
like this:using (var db = new NinjaContext()) { //db.Equipment is a DbSet<Equipment> //id is PrimaryKey of Equipment table var eq = db.Equipment.Find(id); db.Equipment.Remove(eq); db.SaveChanges(); }
相关问答
更多-
这个: @Html.DropDownListFor(model => model.PersonsTitle, new SelectList(Enum.GetValues(typeof(HLRA.eForms.BL.PersonsTitle))))) 应该够了。 我不喜欢你的病情: @if(Model == null) 您应该定义模型,并且应使用默认值设置属性“PersonsTitle”。 This: @Html.DropDownListFor(model => model.PersonsTitle, ne ...
-
我想说你最好的选择是在MVVM模式中使用命令来实现这一点。看起来你正在使用MVVM和代码的组合,并在你的点击事件中实际创建一个新的视图模型实例火灾。 尝试将视图模型绑定到视图中一次作为datacontext在视图后面的代码中,然后尝试更新所选类型。 此外,当您尝试对SelectedType进行更新时 - 使用Snoop查看您的View - 查看SelectedType属性是否仍绑定到视图。 ICommand UpdateOrgTypeDetail { get;} 然后在视图模型构造函数中声明新实例 Up ...
-
我想在List EF中获取值(I want to get values in List EF)[2024-01-01]
您的实体具有Int64或long定义,而相应的列可以为空,将数据类型更改为: long? Your entity has an Int64 or long defined while the respective column is nullable, change the datatype to: long? -
你只有几个问题: Include的参数应该是实体的导航属性,而不是表名。 换句话说,将其更改为: var list_item = db.mst_item.Include("unit_id").ToList(); 您必须通过导航属性访问此第二个实体的属性。 换句话说,这是您需要的视图代码: @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.item_name) @Html.DisplayFor(modelIte ...
-
这里的问题是我需要一种动态的方法来迭代EntityObjects,它们也是EDMX中的类型。 我需要列出实体名称及其属性。 非常感谢Craig Stuntz带领我走上正确的道路来解决这个问题。 这是我想出的最终代码来解决我的问题。 EmployeesEntities context = new EmployeesEntities(); MetadataWorkspace workspace = context.MetadataWorkspace; workspace.LoadFromAssembly(As ...
-
EF5:无法从模型中获取StoreGeneratedPattern属性(EF5: Can't Get StoreGeneratedPattern Property From Model)[2022-08-12]
Pawel的问题让我检查了edmx并找到答案。 我在我的问题中说我可以查看模型(UI)并看到有问题的列是一个标识列。 事实上,事实并非如此。 在任何情况下,我都希望StoreGeneratedPattern属性存在,因为枚举包含所有可能的值(Identity,Computed,None)。 我的实际目标是检测主键列/属性不是标识的所有实体。 我通过后续测试发现,StoreGeneratedPattern属性仅存在于identity&computed; 如果列不是存储生成的,则它不存在(从不引用StoreGe ... -
只需删除此: //Make sure entity knows EquipmentOwned has been modified db.Entry(ninja.EquipmentOwned).State = EntityState.Modified; 这会导致错误。 EquipmentOwned是List
。 它不是EF Entry ,因此不会直接跟踪它。 当您从此类集合中删除实体时, EF知道此特定忍者与此特定设备之间不存在任何关系。 它不会从数据库 ... -
您的视图模型和视图存在问题。 您可以在此处参考类似情况的基本结构。 我还建议你看看音乐商店应用教程,它可以很好地解释这些概念。 您正走在正确的道路上,只需在ViewModel和View中进行一些更改,您就完成了。 一旦结构得到修复,您将有更具体的问题,社区将乐意回答。 编辑:只是一些帮助,有这个Nuget包“EntityFramework.Sample”,它给你样本模型和演示DbContext设置来玩。 There are problems with your view model and view. Yo ...
-
你可以这样做: List
someList = new List (); db.Points.Where(m => someList.Contains(m.Description)) 多个条件: db.Points .Where(m => someList.Contains(m.Description)) .Where(m => m.someprop == true) 要么 db.Points .Where(m => someList.Contains(m.Descr ... -
首先由EF代码在数据库中生成的MVC模型中的字节列表(List of bytes in MVC model not generated in database by EF code first)[2022-02-03]
我不认为EF知道如何映射List。 我会尝试byte[] 。 [Required] [Display(Name = "List of items included into product name")] public byte[] ProductsNameCompositionList { get; set; } I don't think EF knows how to map List . I would try just byte[]. [Required] [D ...