MongoDB - 按上次嵌入文档获取文档

时间:2017-08-04 14:03:31

标签: mongodb

我正面临一个问题。我目前正在创建一个运行任务的应用。某些任务可能会导致错误。在这种情况下,我在我的mongo db中存储我的任务状态。

我想创建一个加载每个错误任务并重新运行它们的作业。

查看数据结构:

<div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4 class="modal-title">Results</h4>
                </div>
                <div class="modal-body">
                    <div class="input-group input-group-md">
                        <span class="input-group-addon" id="sizing-addon1"><i class="fa fa-user" aria-hidden="true"></i></span>
                        <input type="text" class="form-control" id="contactModalPhoneInput" placeholder="Name" aria-describedby="basic-addon1" disabled/>
                    </div>
                    <br>
                    <button type="button" class="btn btn-danger pull-right" id="btnChangePhone">Change</button>

                    <br>
                    <div>
                        <h5>No Person Associated</h5>
                        <div>
                            <select id="dropdownPerson" title="Choose a Person"></select>
                            <div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-danger" data-dismiss="modal" onclick="updatePerson()">Update</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>

                </div>
            </div>

我想做的只是加载最后一个嵌入文档(而且只是最后一个)$('#dropdownPerson').multiselect(); for (let i = 0; i < 10; i++) { let elem = document.createElement("option"); elem.textContent = i; document.getElementById("dropdownPerson").appendChild(elem); } I am also adding the following to the html: <!-- jQuery--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script> <!-- Bootstrap--> <link href="../css/bootstrap.min.css" rel="stylesheet"> <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <!-- Multiselect--> <link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css" rel="stylesheet" type="text/css" /> <script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js" type="text/javascript"></script> <!-- Imports--> <script src="../js/globals.js"></script> <script src="../js/utils.js"></script> <script src="../js/functions.js"></script> <script src="../js/populate.js"></script> enter code here { { _id: 1, actions: [ { "name": "action_1", "statuses": [ { "date": "2017-08-04 15:37:54.000", "status": "pending" }, { "date": "2017-08-04 15:37:56.000", "status": "running" }, { "date": "2017-08-04 15:37:58.000", "status": "passed" } ] } ] }, { _id: 2, actions: [ { "name": "action_1", "statuses": [ { "date": "2017-08-04 15:37:54.000", "status": "pending" }, { "date": "2017-08-04 15:37:56.000", "status": "running" }, { "date": "2017-08-04 15:37:58.000", "status": "error" } ] } ] }, { _id: 3, actions: [ { "name": "action_1", "statuses": [ { "date": "2017-08-04 15:37:54.000", "status": "pending" }, { "date": "2017-08-04 15:37:56.000", "status": "error" }, { "date": "2017-08-04 15:37:56.000", "status": "running" }, { "date": "2017-08-04 15:37:58.000", "status": "passed" } ] } ] }, { _id: 4, actions: [ { "name": "action_1", "statuses": [ { "date": "2017-08-04 15:37:54.000", "status": "pending" }, { "date": "2017-08-04 15:37:56.000", "status": "error" }, { "date": "2017-08-04 15:37:56.000", "status": "running" }, { "date": "2017-08-04 15:37:58.000", "status": "passed" } ] }, { "name": "action_2", "statuses": [ { "date": "2017-08-04 15:37:54.000", "status": "pending" }, { "date": "2017-08-04 15:37:56.000", "status": "error" } ] } ] } } 的文档。

最后,查询将返回包含ID actions.statuses.statuserror的文档。

有没有办法说&#34;只有最后一个嵌入的文件&#34; ?

谢谢:)

1 个答案:

答案 0 :(得分:1)

根据@NeilLunn解决方案,我将状态反向存储,以构建0位置的查询,如下所示:db.my_collection.find({"actions.0.statuses.0.status": "error"})

感谢他!