我在集合中有以下文档:
"_id" : "2",
"workspace" : [{
"name" : "1",
"widgets" : [ ]
},{
"name" : "2",
"widgets" : [ ]
},{
"name" : "3",
"widgets" : [ ]
},{
"name" : "4",
"widgets" : [ ]
}
]}
Run Code Online (Sandbox Code Playgroud)
如何{id: "1", blabla: "blabla"}在"小部件"中插入"名称"3?
我创建了这个架构mongoose Schema:
socialAccount = new Schema({
socialNetwork : { type : String , required : true},
userid : { type : Number, required : true },
username : String
},{_id : false});
person = new Schema({
id : { type : Number, unique : true, required : true , dropDups : true },
firstname : String,
lastname : String,
socialAccounts : [socialAccount],
updated : { type : Date, default : Date.now },
enable : { type : Boolean …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,其中一篇文章可以链接到多个平台.
文章包含平台和平台列表,还包含文章列表.
有关更多详细信息,请查看几个月前我问过的stackoverflow问题.
问题是如何创建文章和实现文章和平台之间的NN关系.
我有创建文章和删除文章设置,以便列表在平台中更新.
如何实现编辑文章,以便更新哪些平台链接到文章?
为了创建和编辑链接平台,我使用下拉菜单,其中可以选择多个选项.必要的代码可以在先前链接的问题中找到.
在mongoose上,有一个很好的选择,默认情况下使用该select: false选项从查询中删除一些字段.
例如:
var FileSchema = new Schema({
filename: String,
filesize: Number,
base64Content: {type: String, select:false}
});
[...]
FileModel.find({}, function(err, docs) {
// docs will give me an array of files without theirs content
});
Run Code Online (Sandbox Code Playgroud)
现在,我如何在子文档数组的字段中使用相同的选项?
(即在以下示例中,设置select: false为comments字段)
var PostSchema = new Schema({
user: ObjectId,
content: String,
createdAt: Date,
comments: [{
user: ObjectId,
content: String,
createdAt: Date
}]
});
[...]
FileModel.find({}, function(err, docs) {
// docs will give me an array of files without …Run Code Online (Sandbox Code Playgroud) 我目前有一个架构,目前看起来像:
var User = new Schema({
id: String,
firstName: String,
lastName: String,
password: String,
username: String,
position: [{
title: String,
location: String,
start: String,
term:Number,
description:String,
date: {type: Date, default: Date.now}
}]
});
Run Code Online (Sandbox Code Playgroud)
我有两个用户,每个用户有两个嵌入式位置文档.
用户1:
"position" : [
{
"title" : "Web Developer",
"location" : "Dublin",
"start" : "May 2017",
"term" : 6,
"description" : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus quis erat vitae dsit amet, consectetur adipiscing elit. Vivamus quis erat vitae dolor tempus euismod …Run Code Online (Sandbox Code Playgroud) ProductCollection:
{
_id: { ObjectId('x1')},
products: [
{ listPrice: '1.90', product: {id: 'xxx1'} },
{ listPrice: '3.90', product: {id: 'xxx2'} },
{ listPrice: '5.90', product: {id: 'xxx3'} }
]
},
{
_id: { ObjectId('x2')},
products: [
{ listPrice: '2.90', product: {id: 'xxx4'} },
{ listPrice: '4.90', product: {id: 'xxx5'} },
{ listPrice: '5.90', product: {id: 'xxx6'} }
]
},
Run Code Online (Sandbox Code Playgroud)
我想从集合 (x1) 中删除子文档 (xxx3),然后尝试以下操作:
ProductCollection.update(
{
"_id": mongoose.Types.ObjectId('x1')
},
{
$pull : { 'products.product': {id: 'xxx3' } }
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用。谁能帮帮我吗?谢谢
mongodb ×6
subdocument ×6
mongoose ×4
node.js ×3
async.js ×1
for-loop ×1
javascript ×1
meteor ×1
pull ×1
relationship ×1
schema ×1