我有一个类似这样的文档的收藏集
import { MatDialog } from '@angular/material';
....
public dialog;
constructor(public dialog: MatDialog) {}
openSomeModal() {
const data = {};
const dialogRef = this.dialog.open(yourModalComponent, {
data: data,
width: '400px',
panelClass: 'any-class',
});
}
.....
我只想获取所有嵌入式文档“ StopDetails”。我试图调整下面的代码行,但是我无法在$ query“ StopsDetails”中写什么,以便仅获取StopsDetails嵌入式文档。 请帮忙!!!
{
"_id": ObjectId("5b4dd622d2ccda10c00000f0"),
"Code": "Route-001",
"Name": "Karan Nagar - Jawahar Nagar",
"StopsDetails": [
{
"StopId": "cb038446-bbad-5460-79f7-4b138024968b",
"Code": "Stop- 001",
"Name": "Lane market",
"Fee": "600"
},
{
"StopId": "2502ce2a-900e-e686-79ea-33a2305abf91",
"Code": "Stop-002",
"Name": "City center",
"Fee": "644"
}
],
"StopsTiming" :
[
....
]
}
答案 0 :(得分:1)
YourModel.findOne({_id:yourId},function(err,result){
console.log(result); //will return all data
console.log(result.StopsDetails); //will retrun your stop details
}
答案 1 :(得分:1)
您需要在查询的第二个参数中使用projection
$query = ['_id' => new MongoDB\BSON\ObjectID($this->id)]
$projection = ['StopsDetails' => true]
$cursor = $this->collection->find($query, $projection);
类似于javascript查询
db.collection.find({ '_id': ObjectId }, { 'StopDetails': 1 })
答案 2 :(得分:1)
如果要在MongoDB中获取特定的嵌入式字段,则可以使用投影
db.col.find({"_id" : ObjectId("5b4dd622d2ccda10c00000f0")},{"StopsDetails" : 1});