Best way to delete documents in marklogic which are having multiple version - xquery

We had documents in database which are having multiple versions. For ex
Documents URI are like below :-
/PremierLeague/Manutd/players.xml-version-2
/PremierLeague/Manutd/players.xml-version-3
/PremierLeague/Manutd/players.xml-version-4
.
.
.
.
.
/PremierLeague/Manutd/players.xml-version-998
/PremierLeague/Manutd/players.xml-version-999
/PremierLeague/Manutd/players.xml-version-1000
We need to find the count of all the documents like this and then delete all the versions of document which are above ".version-20".
For example
/PremierLeague/Manutd/players.xml-version-19 --> NOT DELETE
/PremierLeague/Manutd/players.xml-version-20 --> NOT DELETE
/PremierLeague/Manutd/players.xml-version-21 --> TO BE DELETED
/PremierLeague/Manutd/players.xml-version-22 --> TO BE DELETED
.
.
.
.
/PremierLeague/Manutd/players.xml-version-999 --> TO BE DELETED
/PremierLeague/Manutd/players.xml-version-1000 --> TO BE DELETED
we had multiple directories where multiple documents are present.
As of now, we are counting documents like below
xquery version "1.0-ml";
fn:count(fn:doc("/PremierLeague/Manutd/players.xml-version-*"))
What is the best way to delete documents like this ?

Enable the uri lexicon and use one of the 'uris' or 'directory' functions like cts:uris, not fn:doc -- fn:doc 'opens' the document and returns the document node, not the uri.
You dont want to do that when your deleting documents.
Be careful with the chosen pattern of versions and the requirements -- as-stated you may end up deleting all versions, and documents which happen to have the substring "-version" in them before being versioned would be mis-matched (with your sample code).
Consider the Document Library Services which manages versions of documents including retention policies.

Related

Custom properties/tags is replaced with actual values in MS Word using code or any SDK

I have scenario as I have a template in a Microsoft Word document (.docx) that has placeholders / tags / custom properties that say "{!CompanyName}" since I have a business necessity. This will later, after processing, be combined with the real database table contents or any other source values, and saved as a new Word document (.doc).
Which free SDK works the best for .net core?
Support for the following key feature should be included in the SDK 
tag replacement with actual data
Auto-generated table and rows
Image insertion into the template
Conditional check.
Please make a suggestion here!

Can MLCP read input based on a condition

In marklogic, using MLCP can we read /export/import/copy data based on a condition?
Example : read only files with students subject element has only maths
Yes, you can apply the -query_filter option to restrict documents to those matching the filter query.
https://docs.marklogic.com/guide/mlcp/export#id_66898
The -query_filter option accepts a serialized XML cts:query or JSON cts.query as its value.
Controlling What is Exported, Copied, or Extracted
By default, mlcp exports all documents or all documents and metadata in the database, depending on whether you are exporting in document or archive format or copying the database. Several command line options are available to enable customization.
-query_filter - export/copy only documents matched by the specified cts query. You can use this option alone or in combination with a directory, collection or document selector filter.
-directory_filter - export only the documents in the listed database directories. You cannot use this option with -collection_filter or -document-selector.
-collection_filter - export only the documents in the listed collections. You cannot use this option with -directory_filter or -document_selector.
-document_selector export only documents selected by the specified XPath expression. You cannot use this option with -directory_filter or -collection_filter. Use -path_namespace to define namespace prefixes.

how to wrap my head around a FireStore query

I am new in Flutter - Firestore
I am learning flutter with firebase and creating a sample dating app
I have a list of users that I get in a stream and display it using List view
Firestore.instance.collection('users').snapshots()
I have learnt to filter this like so
.where((user) => user.age < settings.agemax && user.age > settings.agemin))
and all this works.
I also have a subcollection called shortlist (list of users that current user has shortlisted) that I get using,
Firestore.instance.collection('users').document(uid).collection('shortlist').snapshots()
Now I am trying to redefine my first query GetUsers with filters based on following
How do I exclude shortlisted users that I am fetching in a stream from all users stream
Similarly would also need to filter out "matched users" and "Blocked / declined" users as well !
I believe my question is how do I query Users Collection and exclude records with uid's that contained in a Shortlist subcollection. I am planning to use the same logic for matches and blocked !? am I on the right track ?
also ... do I need to refetch all records when a users shortlists/matches/blocks someone, as the stream would change or is there a way to remove that one record from the listview without rebuilding, may be I should separate this question in two.
If I understand correctly you are looking for the (just introduced) not-in operator, so I recommend also checking out this blog post.
I expect that this operator hasn't landed in the Flutter libraries yet, as that may take some time. I recommend checking the upcoming releases to see when it lands, or checking/filing an issue on the repo.
Until then, there's no way to exclude results from a query, so you will have to exclude the items from the stream of results in your application code.

How to query sub-collection of particular id only from particular parent collection?

I have collections named quests and quizzes, which have sub-collections inside their documents named published which will contain different published versions of the corresponding document.
quests/
published/
quizzes/
published/
quests and quizzes are also tagged and these tags are copied to published document. So all published documents have a tags field(an Array of Strings).
I want to query all the published quests which are related to given tag.
Something along this
db
.collectionGroup('published')
// This query is not correct, regex syntax doesn't work here for path.
// I have added it to show that I want to query only the published
// documents inside quests.
.where(
firebase.firestore.FieldPath.documentId(),
'==',
'/quests/*/published/*'
)
.where(
'tags',
'array-contains',
'kinematics'
)
--- EDIT ---
this is structure of quests, and quizzes also has the same structure.
and it also has tags field.
quest when version 1.0 was published
quest when version 2.0 was published
I want to fetch all the published quests that are related some particular tag, so here quests//published/2.0 should be one of the resulting documents, if I query with 'physics' tag, as version 1.0 it is not related only to 'kinematics'. Screenshot of only one quest is added here. there are multiple quests with multiple published version inside them relating to tag I want to query. I want to fetch all of them, excluding documents from quizzes//published/ with a single query.
This part of your query is not possible:
.where(
firebase.firestore.FieldPath.documentId(),
'==',
'/quests/*/published/*'
)
FieldPath.documentId() just can't be used as a filter in collection group queries. It's a known limitation. There are also no wildcard matches in Firestore, at all.
There's another problem with your requirement to "exclude documents from quizzes//published/". Firestore queries also cannot also exclude specific items based on inequality. Please read this other issue for more details. What you can do is store the name of the top-level collection (e.g. "quests") in a field in the documents of a subcollection, then filter to match documents with that field. But you can't exclude documents this way. If you want exclusion, you will need a boolean field that expresses this exclusion, such as "is_not_quiz", and set it appropriately for each document that is not a quiz.

what happens if i don't specify the collection name in Corb

I have a corb script to run node replace on the xml files.
If I don't specify the collection, will it remove the documents from the existing collections?
If you are altering the document with xdmp:node-replace(), then the document will remain in it's collections and you do not need to worry about setting/adding it back.
If you are using xdmp:document-insert() to replace the document at the current URI, then you do need to specify the collection(s), otherwise it will be removed from the existing collections.
However, you can use xdmp:document-get-collections() to retrieve the sequence of collections for the URI and use it for the 4th parameter of xdmp:document-insert()
xdmp:document-insert($URI, $doc, (), xdmp:document-get-collections($URI))
Its better to provide an empty collection value, while doing the node-replace so it doesn't alter the existing collections of the document. Not defining this attribute is throwing errors while running the script.

Resources