基于多个子文档的MongoDB / Mongoose查询(MongoDB/Mongoose query based on multiple subdocuments)
我是MongoDB和Mongoose的新手。 我目前正在构建一个具有客户端集合的应用程序,该集合包含客户端拥有的一系列帐户。
我想根据客户端帐户的具体情况查询集合。 例如,我需要返回以下客户端:
- clientType:“标准”
- 有两个帐户:
- accountType:“FML”,余额:$ gt 3000
- accountType:“OMG”,余额:$ lt 3000
以下是示例文档:
{ clientDetails: { cardNumber: "0123456", firstName: "Bob", lastName: "Dole", clientType: "Standard" }, accounts: [ { accountNumber: "123", accountType: "FML", balance: 4000.00 }, { accountNumber: "234", accountType: "OMG", balance: 2000 } ] }
到目前为止,我只是想出了如何构建一个查询,可以使用accountTypes [“FML”,“OMG]获取clientType”Standard“的客户端,但我无法弄清楚如何为特定的指定平衡条件accountTypes。
ClientModel .find({ "clientDetails.clientType": "Standard", "accounts.accountType": { $all ["FML", "OMG"] }) .then( function(){ //etc..}, function(err){ //etc...} );
I'm brand new to MongoDB and Mongoose. I'm currently building an app that has a client collection that contains an array of accounts that the client has.
I'd like to query the collection based on specifics of the accounts the client has. For example, I need to return the client that is:
- clientType: "Standard"
- Has two accounts:
- accountType: "FML", balance: $gt 3000
- accountType: "OMG", balance: $lt 3000
Below is a sample document:
{ clientDetails: { cardNumber: "0123456", firstName: "Bob", lastName: "Dole", clientType: "Standard" }, accounts: [ { accountNumber: "123", accountType: "FML", balance: 4000.00 }, { accountNumber: "234", accountType: "OMG", balance: 2000 } ] }
So far, I've only figured out how to build a query that can get a client of clientType "Standard" with accountTypes ["FML","OMG] but I can't figure out how to specify the balance condition for the specific accountTypes.
ClientModel .find({ "clientDetails.clientType": "Standard", "accounts.accountType": { $all ["FML", "OMG"] }) .then( function(){ //etc..}, function(err){ //etc...} );
原文:https://stackoverflow.com/questions/43766481
最满意答案
我之前已在这篇文章中回答了这个问题。 基本上来自hasOne和hasMany的声明它们在目标模型上创建FK。 如果你认为FK在1:1的关系上停留在原始模型上,你必须使用belongsTo,甚至sequelize docs建议在这里 。
尽管它被称为HasOne关联,但对于大多数1:1关系,您通常需要BelongsTo关联,因为BelongsTo会在hasOne将添加到目标上的源上添加foreignKey。
对于1:N之间的关系,除了hasMany之外没有其他选项,正如我所说,它在目标模型上创建了FK,因为它是如何工作的1:N关系。
I already answered this question before on this post. Basically the declarations from hasOne and hasMany they create the FK on the destination model. If you one that FK stays on the origin model on a relation 1:1, you have to use belongsTo, even sequelize docs recommend that here.
Even though it is called a HasOne association, for most 1:1 relations you usually want the BelongsTo association since BelongsTo will add the foreignKey on the source where hasOne will add on the target.
And for a relation between 1:N you have no other option than hasMany, and as I said, it create the FK on the destination model, because thats how it works a 1:N relation.
相关问答
更多-
如何sequelize作品?(How sequelize works?)[2022-12-22]
对于你的版本“sequelize”:“^ 4.13.2” : classMethods和instanceMethods被删除。 以前: const Model = sequelize.define('Model', { ... }, { classMethods: { associate: function (model) {...} }, instanceMethods: { someMethod: function () { ...} ... -
阅读手册https://book.cakephp.org/3.0/en/orm/associations.html 。 仔细阅读整页。 className :与当前模型关联的表的类名。 如果您要定义“User hasOne Address”关系,则className键应该等于“Addresses”。 条件 :find()兼容条件的数组,例如['Addresses.primary'=> true] 定义关联所需的类名称和条件。 $this->hasOne('Foo', [ 'class ...
-
您需要在create方法中包含关联才能添加它。 Route .create({ name: 'route1', customers: [{ name: "customer1" }, { name: "customer2" }] }, { include: [ Customer ] }); 这将创建表中的客户。 如果要添加与两个现有客户关联的路由,则需要采用不同的方法。 Route.create( { name: "route1" } ) .th ...
-
我之前已在这篇文章中回答了这个问题。 基本上来自hasOne和hasMany的声明它们在目标模型上创建FK。 如果你认为FK在1:1的关系上停留在原始模型上,你必须使用belongsTo,甚至sequelize docs建议在这里 。 尽管它被称为HasOne关联,但对于大多数1:1关系,您通常需要BelongsTo关联,因为BelongsTo会在hasOne将添加到目标上的源上添加foreignKey。 对于1:N之间的关系,除了hasMany之外没有其他选项,正如我所说,它在目标模型上创建了FK,因为它 ...
-
laravel 4 hasOne / hasMany / belongsTo不起作用(laravel 4 hasOne/ hasMany / belongsTo doesn't work)[2022-09-29]
我的型号代码: hasMany(' ... -
我已经能够从IRC的mickhansen那里得到答案。 mickhansen:“你需要你所查询的那一方的关系,因为你是从汽车方面创造的,你需要汽车 - >细节关系。” 有道理。 这里有一个例子: http : //docs.sequelizejs.com/en/latest/docs/associations/#foreign-keys_1 希望这可以帮助。 I've been able to have an answer from mickhansen on IRC about this. mickhan ...
-
考虑到Brian的帖子的评论,你的问题有点不清楚,但如果你绝对需要用户共享一组给定的位置,那么你最好使用User belongsTo location 。 这将在User创建一个locationId字段,并且您将能够GET api\Users\{userId}\location 最终,您可以设置一个location hasMany User从给定位置检索其中的所有用户 Your problem is a bit unclear, given the comments of Brian's post, but ...
-
关系类型hasOne使用LEFT JOIN ,因此不能支持LIMIT作为选项,因为它会影响整个查询(不仅限制Day而且限制Training )。 有几种方法可以解决您的问题。 最简单的方法是将关联定义为hasMany ,但设置'limit'=>1 。 public $hasMany = array( 'FirstDay' => array( 'className' => 'Day', 'foreignKey' => 'training_id', 'fi ...
-
需要一个简单的例子,用于hasone和hasmany用法[关闭](need simple examples for hasone and hasmany usage [closed])[2022-06-16]
两个具有hasOne关系的模型: class Model_User extends Model_Table { public $table = 'user'; function init() { parent::init(); $this->addField('name'); $this->addField('email'); $this->hasOne('role'); // field role_id in data ... -
目前还没有直接的方式来续集你所要求的 参考https://github.com/sequelize/sequelize/issues/3845 但你可以做到 Group.belongsTo(GroupUser) User.belongsTo(GroupUser) GroupUser.hasOne(Group) GroupUser.hasOne(User) 和嵌套包含(但这不是很有效) User.findAll({include : [{model:GroupUser, include :[{model : ...