gpt4 book ai didi

node.js - 这个查询在 nosql 中是否可行(使用 mongodb)?

转载 作者:可可西里 更新时间:2023-11-01 09:23:46 25 4
gpt4 key购买 nike

我在我的 node.js 应用中使用了 mongoose,基本上有以下模型:

// Define Car model
CarSchema = new Schema({
brand : String,
type: String,
maxSpeed : Number
});
mongoose.model('Car', CarSchema);
// Define User model
UserSchema = new Schema({
lastname : String,
firstname : String,
cars : [CarSchema]
});
mongoose.model('User', UserSchema);

我是 NoSQL 的新手,我真的很想尝试一下,但我首先需要研究这是否真的符合我的需求。

使用上述模型,我是否能够创建一个查询,列出所有拥有特定类型汽车的用户?

最佳答案

我不知道如何在 Mongoose 中做到这一点。但在 mongodb 中是可能的。因此,在 mongodb shell 中查询将如下所示:

db.users.find({"cars.type":"sport"})

以上查询返回在其嵌套汽车集合中拥有类型为“sport”的汽车的所有用户。

Mongodb dot notation文档。

关于node.js - 这个查询在 nosql 中是否可行(使用 mongodb)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5618164/

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