I'm trying to use AQL to get a list of all build not promoted to "release".
Our binaries pass through status integration-> aat -> release
I want to get a list of those with promotion status integration and aat but not release.
One example of a build has statuses:
"statuses" : [ {
"status" : "integration",
"timestamp" : "2016-04-20T08:36:42.009+0000",
"user" : "user",
"ciUser" : "changes",
"timestampDate" : 1461141402009
}, {
"status" : "aat",
"repository" : "repo-aat",
"timestamp" : "2016-04-20T08:56:11.843+0000",
"user" : "user",
"ciUser" : "changes",
"timestampDate" : 1461142571843
}, {
"status" : "aat",
"repository" : "repo-aat",
"timestamp" : "2016-04-20T08:58:55.417+0000",
"user" : "user",
"ciUser" : "changes",
"timestampDate" : 1461142735417
}, {
"status" : "aat",
"repository" : "repo-aat",
"timestamp" : "2016-04-20T09:20:32.619+0000",
"user" : "user",
"ciUser" : "changes",
"timestampDate" : 1461144032619
}, {
"status" : "release",
"repository" : "repo-release",
"timestamp" : "2016-04-20T09:30:12.143+0000",
"user" : "user",
"ciUser" : "changes",
"timestampDate" : 1461144612143
}, {
"status" : "release",
"repository" : "repo-release",
"timestamp" : "2016-04-20T09:40:50.595+0000",
"user" : "admin",
"ciUser" : "changes",
"timestampDate" : 1461145250595
} ],
This build is matched regardless if we set:
{"promotion.status": {"$nmatch":"aat"}}
to
{"promotion.status": {"$nmatch":"release"}}
{"promotion.status": {"$nmatch":"integration"}}
with the request:
builds.find({
"$and" : [
{"name": {"$match": "test"}},
{"created": {"$lt": "2016-12-01"}},
{"promotion.status": {"$nmatch":"release"}}
]
}).include("promotion.status").limit(10)
we get this response:
{
"results" : [ {
"build.created" : "2016-04-20T10:12:46.905Z",
"build.created_by" : "test",
"build.modified" : "2016-04-20T11:45:12.309Z",
"build.modified_by" : "admin",
"build.name" : "user",
"build.number" : "2551",
"build.promotions" : [ {
"build.promotion.status" : "aat"
}, {
"build.promotion.status" : "integration"
} ],
"build.url" : "URL"
} ],
"range" : {
"start_pos" : 0,
"end_pos" : 1,
"total" : 1,
"limit" : 10
}
If you do not need to use wildcards with $nmatch, you can use $ne instead, for example:
builds.find({
"$and" : [
{"name": {"$match": "test"}},
{"created": {"$lt": "2016-12-01"}},
{"promotion.status": {"$ne":"release"}}
]
}).include("promotion.status").limit(10)
With $nmatch, the following will also work:
builds.find({
"$and" : [
{"name": {"$match": "test"}},
{"created": {"$lt": "2016-12-01"}},
{"promotion.status": {"$nmatch":"releas*"}}
]
}).include("promotion.status").limit(10)
What you are trying to do, is to ask Artifactory for the "most recent" status of a build, and filter based on that. However this is not how Artifactory treats your AQL query.
Please note that your build does not have a property "build.promotion.status". Instead, your build has a property of type array with the name "build.promotions". Within this array, any number of promotion history items may be set for your build, including the property "build.promotion.status".
Now suppose your AQL query is going to select builds that have "build.promotion.status" : "aat", what you are really asking Artifactory is this: please return any build for which any of the elements of the build.promotions array has a matching property "build.promotion.status" : "aat".
So eventhough the build #2551 in your example has been promoted from "aat" to "released", you are asking AQL if it did - at any point in time - have promotion status "aat", which it did.
To add to the confusion, when you include("promotion.status"), you are going to see a filtered subset of the promotion history items.
If you are trying to work around this by asking for the reverse question: which builds do not have any build status history item with "build.promotion.status" = "released", even if that would be possible with AQL, it would not tell you what the current status is. Nor would it work correctly if you build is ever "Rolled-back".
I think JFROG should actually introduce a "build.promotion.status" field, which does what people reasonably expect: to give you the current status to display and to query on. Until that time, the only solution I can think of is to fetch all the build promotion items and then do the magic in a higher order language.
Related
I have a document user that have a schema as:
{
"_id" : ObjectId("57b2d706f61d04e8d99dd983"),
"addressesAsVendor" : [
{
"_id" : "V1",
"addressLine1" : "al1",
"addressLine2" : "al2",
"street" : "street",
"city" : "city",
"country" : "IN",
"pincode" : "490020",
"location" : {
"latitutde" : "lat1",
"longitude" : "lon1"
}
},
{
"_id" : "V2",
"addressLine1" : "al1",
"addressLine2" : "al2",
"street" : "street",
"city" : "city",
"country" : "IN",
"pincode" : "490020",
"location" : {
"latitutde" : "lat2",
"longitude" : "lon2"
}
}
]
}
Now let's suppose I want to update the V1 id of the addressesAsVendor array which is inside the user Id 57b2d706f61d04e8d99dd983 with the data:
{
"addressLine1" : "al1 new",
"addressLine2" : "al2 new",
"street" : "street new",
"city" : "city new",
"country" : "IN new",
"pincode" : "490020 new",
"location" : {
"latitutde" : "lat1 new",
"longitude" : "lon1 new"
}
}
So the new user doc will look like:
{
"_id" : ObjectId("57b2d706f61d04e8d99dd983"),
"addressesAsVendor" : [
{
"_id" : "V1",
"addressLine1" : "al1 new",
"addressLine2" : "al2 new",
"street" : "street new",
"city" : "city new",
"country" : "IN new",
"pincode" : "490020 new",
"location" : {
"latitutde" : "lat1 new",
"longitude" : "lon1 new"
}
},
{
"_id" : "V2",
"addressLine1" : "al1",
"addressLine2" : "al2",
"street" : "street",
"city" : "city",
"country" : "IN",
"pincode" : "490020",
"location" : {
"latitutde" : "lat2",
"longitude" : "lon2"
}
}
]
}
How this can be achieved in MongoDb and what's the best way of keeping multiple addresses, which can be easily shown in the Address page of the user and also minimum in load, I mean will be easy to access when required.
Please shed your views.
:)
You can do the update using the positional $ operator:
var data = {
"addressLine1" : "al1 new",
"addressLine2" : "al2 new",
"street" : "street new",
"city" : "city new",
"country" : "IN new",
"pincode" : "490020 new",
"location" : {
"latitutde" : "lat1 new",
"longitude" : "lon1 new"
}
};
collection.update(
{ "addressesAsVendor._id" : "V1" },
{ "$set": { "addressesAsVendor.$": data } },
function(err, result) {
if (err) return handleError(err);
console.log(result);
}
)
The positional operator in the above saves the index (0 in the case above) of the element from the array that matched the query. This means that if you knew the position of the element beforehand (which is nearly impossible in a real life case), you could just change the update statement to: { "$set": { "addressesAsVendor.0": data } }.
Since the positional $ operator acts as a placeholder for the first element that matches the query document, and the array field must appear as part of the query document hence the query { "addressesAsVendor._id" : "V1" } is essential to get the $ operator to work properly.
Please note that the positional $ operator (for now) updates the first relevant document ONLY, there is a JIRA ticket for this.
For your follow-up question which seeks to find the best way of keeping multiple addresses, which can be easily shown in the Address page of the user and also minimum in load:
Your current schema is a better approach than creating a separate collection of addresses since separate collections require more work i.e. finding a user + its addresses is two queries and requires extra work whereas the above schema embedded documents are easy and fast (single seek). There are no big differences for inserts and updates. So, separate collections are good if you need to select individual documents, need more control over querying, or have huge documents. Embedded documents are good when you want the entire document, the document with a $slice of the embedded addressesAsVendor, or with no addresses at all.
As a general rule, if you have a lot of "addresses" or if they are large, a separate collection might be best.
Smaller and/or fewer documents tend to be a natural fit for embedding.
You can use updateOne (MongoDB query) to update the document.
NodeJS equivalent is:-
collection.update(criteria, update[[, options], callback]);
Please change the object id accordingly.
db.address.updateOne({ "_id" : ObjectId("57c04425c400a6b59c9bc1ee"), "addressesAsVendor._id" : "V1" }, { $set: { "addressesAsVendor.$.addressLine1" : "al1 new",
"addressesAsVendor.$.addressLine2" : "al2 new",
"addressesAsVendor.$.street" : "street new",
"addressesAsVendor.$.city" : "city new",
"addressesAsVendor.$.country" : "IN new",
"addressesAsVendor.$.pincode" : "490020 new",
"addressesAsVendor.$.location" : {
"latitutde" : "lat1 new",
"longitude" : "lon1 new"
}
} });
I have documents that contains a object which the attributes are editable (add/delete/edit) in runtime.
{
"testIndex" : {
"mappings" : {
"documentTest" : {
"properties" : {
"typeTestId" : {
"type" : "string",
"index" : "not_analyzed"
},
"createdDate" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"designation" : {
"type" : "string",
"fields" : {
"raw" : {
"type" : "string",
"index" : "not_analyzed"
}
}
},
"id" : {
"type" : "string",
"index" : "not_analyzed"
},
"modifiedDate" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"stuff" : {
"type" : "string"
},
"suggest" : {
"type" : "completion",
"analyzer" : "simple",
"payloads" : true,
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50,
"context" : {
"typeTestId" : {
"type" : "category",
"path" : "typeTestId",
"default" : [ ]
}
}
},
"values" : {
"properties" : {
"Att1" : {
"type" : "string"
},
"att2" : {
"type" : "string"
},
"att400" : {
"type" : "date",
"format" : "dateOptionalTime"
}
}
}
}
}
}
}
}
The field values is a object that can be edited throug typeTest, so if I change something in typeTestit should be reflected here. If i create a new field theres no problem, but it should be possible to edit or delete existing fields in typeTest. For example If I delete values.att1 all documentTest should lose these, as well as the mapping should be updated.
For what I saw, we cannot do these without reindexing. So for now my solution is to remove the fields in elastic search just like mentioned in this question and have a worker do the reindexing time to time if needed.
This does not seems to me a "solution". Is there a better way to have document of this type in elasticsearch? with this flexibility without having to reindex time to time?
You can use the Update API to delete, add or modify a field.
The issue is docs are immutable in elasticsearch, so when you make some changes with the update API it is executed in a manner mark as deleted to old one and add a new one with the updates.
The deletion and the creating the new documents is transparent to you, so you do not have to reindex or do any other thing. Down side is if you are planning to modify very large numbers of documents (like an update query to modify 5mil documents.) it will be very I/O intensive for the nodes.
BTW, this is also applies to deletions
I have a mongodb collection data as per below;I want to group by EmployeedID( i.e 0001) and then sort(by age)
{
"_id" : ObjectId("54d0512191a4da7736e9db43"),
"EmployeeID" : "0001",
"Speciality" : "xxx",
"Code" : "P",
"Age" : 8
}
/* 1 */
{
"_id" : ObjectId("54d0512191a4da7736e9db44"),
"EmployeeID" : "0002",
"Speciality" : "yyyyy",
"Code" : "P",
"Age" : 6
}
/* 2 */
{
"_id" : ObjectId("54d0512191a4da7736e9db45"),
"EmployeeID" : "0001",
"Speciality" : "zzz",
"Code" : "P",
"Age" : 5
}
I know I can group using the following way.
collection.aggregate([
{$match:{"EmployeeId":0001}},
{$group:{"_id":"$EmployeeID",
"speciality":{$push:"$Speciality"},
"Code":{$push:"$Code"},
"Age":{$push:"$Age"}}}
])
But how can I using $sort here? SO my result can be something like below;
[{ "EmployeeID" : "0001",
"speciality" : [ "zzz","xxx"],
"Code" :[ "P","P"],
"Age" : [5,8]
}]
You can sort the document prior to the grouping stage:
collection.aggregate([
{$sort: {_id: -1}},
{$match:{"EmployeeId":0001}},
{$group:{"_id":"$EmployeeID",
"speciality":{$push:"$Speciality"},
"Code":{$push:"$Code"},
"Age":{$push:"$Age"}}}
])
Sorting prior to grouping may exceed mongo's memory when dealing with large collections. Fortunately, you can set allowDiskUse to true to allow mongo to write temporary files.
I am trying to remove the lowest homework score.
I tried this,
var a = db.students.find({"scores.type":"homework"}, {"scores.$":1}).sort({"scores.score":1})
but how can I remove this set of data?
I have 200 pieces of similar data below.
{
"_id" : 148,
"name" : "Carli Belvins",
"scores" : [
{
"type" : "exam",
"score" : 84.4361816750119
},
{
"type" : "quiz",
"score" : 1.702113040528119
},
{
"type" : "homework",
"score" : 22.47397850465176
},
{
"type" : "homework",
"score" : 88.48032660881387
}
]
}
you are trying to remove an element but the statement you provided is just to find it.
Use db.students.remove(<query>) instead. Full documentation here
Given this Person collection:
{
"_id" : ObjectId("4f8e95a718bcv9c74da1e6511a"),
"name" : "John",
"hobbies" : [{
"id" : 001,
"name" : "reading",
"location" : "home"
},{
"id" : 002,
"name" : "sport",
"location" : "outside"
}]
}
and these new/edited Hobby objects:
{
"name" : "walking",
"location" : "outside"
}
and
{
"id" : 001,
"name" : "reading",
"location" : "outside"
}
If I know the Person that I want to manage, what is be the best way to upsert embedded objects?
Currently my approach is to find the Person object, make the required modifications to it in my code, and then save it back to the DB. This works. But I'd like to simplify and reduce the number of round trips to the database.