Selector query using search index - cloudant

Given the document:
{
docId: "a1234",
doc_type: "asset",
}
I have the following search index function in _design/assets with index name search_asset:
function(doc) {
if (doc.doc_type !== 'asset') return;
index('docId', doc.docId, { store: 'true', facet: 'true' });
}
However, when I try to search using the cloudant selector query:
{
selector: {
docId: "a1234"
},
fields: ["docId"]
}
It gives me this error: Error running query. Reason: (no_usable_index) There is no index available for this selector.
I also added the use_index option but that doesn't seem to work either:
{
selector: {
docId: "a1234"
},
fields: ["docId"],
opts: {
use_index: ["_design/assets", "search_assets"]
}
}

I think you are mixing Cloudant Query (aka Mango) and Cloudant Search (Lucene behind the scenes).
The selector syntax belong to the former, whereas the index function as described looks like a Search/Lucene index.
Cloudant Search -- both how to index and how to query is well documented here.
If you prefer the newer Cloudant Query, that's documented here.
There is some overlap, and Cloudant Query's text indexes are implemented on top of Cloudant Search, to confuse matters further.

Related

Use nestjs to add unique ObjectID to each document that doesn't have one

Just looking for a bit of guidance. I am connected to my local mongoDB instance and I am trying to build a query that will add a unique objectID to every object in an array that doesn't have an _id already. I am trying to do this in nestjs so I can utilize javascript and get a unique ObjectID. Here is what I have so far.
const temp = await this.vehiclesRepository.updateMany(
{
$stipulations: [],
},
{
$set: {
$stipulations: {
$function: {
body: "function (ar) {return ar.map(x => { if(x.hasOwnProperty('_id')) return x; else {x['_id']=new ObjectId(); return x;}})}",
args: ['$stipulations'],
lang: 'js',
},
},
},
},
);
public async updateMany(filter: FilterQuery<T>, doc: UpdateQuery<T>) {
this.model.updateMany(filter, doc, { upsert: true, multi: true });
}
Can anyone tell me where I am going wrong with this? Based on my research this seems like the proper method to accomplish this task.
The $function operator is an aggregation operator. You should use it within an aggregation pipeline. There is also a $set operator specifically meant for aggregations.
You should combine the $function and $set (aggregation) operators within an aggregation pipeline.
I was able to answer my question in the MongoDB shell, with no javascript like so
db.vehicles.find().forEach(function(doc){
if(doc.stipulations != null) {
if(doc.stipulations.length > 0) {
doc.stipulations.forEach(function(stip) {
if(stip._id == null) {
stip._id = new ObjectId();
}
});
}
}
db.vehicles.updateOne({_id: doc._id}, {$set: doc});
});
For optimization it is possible to filter purely in find(), but for this one time query I am not posting that.

Filter react-admin query by dates

I am using react-admin's useGet... query to gather data from my rails backend. The main problem here is the filter property (the last pair of curly braces in the useGetList operation). How can i filter the data by Dates (like get only transactions of the last month etc.)
This is the current (working) approach:
const { data, loading, error } = useGetList(
'transactions',
{ page: 1, perPage: 10000 },
{ field: 'id', order: 'ASC' },
{},
)
if (loading) return <p>Loading</p>
if (error) return <p>Error</p>
if (!data) return null
The entries in the database all have a createdAt and updatedAt property.
My approach would be to create a filter like this:
// constraints could be dates that I can easily set beforehand
{
{'createdAt', desiredLowerTimeConstraint, operation: '<='},
{'createdAt', desiredUpperTimeConstraint, operation: '<='}
}
The react-admin documentation is quite sparce with the filter property, I couldn't find good examples for how these objects are supposed to look like.
It all depends on how your API expects filters to look like.
For instance, in REST APIs served by JSONServer, a _lte suffix on a query string parameter name indicates that you want results "Less Than or Equal to" the value:
GET /transactions?createdAt_lte=2019-12-05
Provided you use the ra-data-simple-rest, you can craft this request by passing the parameter in the filter:
const { data, loading, error } = useGetList(
'transactions',
{ page: 1, perPage: 10000 },
{ field: 'id', order: 'ASC' },
{ createdAt_lte: '2019-12-05' },
)
If your API behaves differently, then you may use the same syntax for useGetList, and transform the parameter in your dataProvider before it's sent to the API.

Tell apollo-client what gets returned from X query with Y argiments?

I have a list of Items of whatever type. I can query all of them with query items or one with query item(id).
I realize apollo can't know what will be returned. It knows the type, but it doesn't know the exact data. Maybe there is a way not to make additional request? Map one query onto another?
Pseudo-code:
// somewhere in Menu.tsx (renders first)
let items = useQuery(GET_ITEMS);
return items.map(item => <MenuItemRepresenation item={item} />);
// meanwhile in apollo cache (de-normalized for readability):
{ ROOT_QUERY: {
items: [ // query name per schema
{ id: 1, data: {...}, __typename: "Item" },
{ id: 2, data: {...}, __typename: "Item" },
{ id: 3, data: {...}, __typename: "Item" },
]
}
}
// somewhere in MainView.tsx (renders afterwards)
let neededId = getNeededId(); // 2
let item = useQuery(GET_ITEM, { variables: { id: neededId } } );
return <MainViewRepresentation item={item} />;
Code like this will do two fetches. Even though the data is already in the cache. But it seems apollo thinks on query level. I would like a way to explain to it: "If I make item query, you need to look over here at items query you did before. If it has no item with that id go ahead and make the request."
Something akin to this can be done by querying items in MainView.tsx and combing through the results. It might work for pseudo-code, but in a real app it's not that simple: cache might be empty in some cases. Or not sufficient to satisfy required fields. Which means we have to load all items when we need just one.
Upon further research Apollo Link looks promising. It might be possible to intercept outgoing queries. Will investigate tomorrow.
Never mind apollo link. What I was looking for is called cacheRedirects.
It's an option for ApolloClient or Cache constructor.
cacheRedirects: {
Query: {
node: (_, args, { getCacheKey }) => {
const cacheKey = getCacheKey({
__typename: "Item",
id: args.id,
});
return cacheKey;
},
},
},
I'd link to documentation but it's never stable. I've seen too many dead links from questions such as this.

MongoDB using $exists and $and still return documents without field

I have flat objects stored in mongo and one of them have the field00048 attribute but my query is still returning entries without field00048. Can someone tell me what's wrong with my mongo query? I have attached a picture of the database below to show the structure.
db.QA_Book_01.find({
field00048: { $exists: true }},
{
$and:[
{ 'entryTypeId': 'Entry_Type_01' },
{ 'field00048': { $ne : 'Closed' }}]
}).count();
Out of 3 records, there is only one with the field 'field00048'. When I change the entryTypeId to Entry_Type_02, it still retrieves the record with the field 'field00048'. Not sure what's going on here.
By default, there is an and operation in find query of MongoDB so the proper syntax for applying an and operation in find query is mentioned below.
db.users.find( { $and: [ {field00048: { $exists: true }},
{ 'entryTypeId': 'Entry_Type_01' },
{ 'field00048': { $ne : 'Closed' }} ] } )

Mongoose - Remove several objects from an array (not exact match)

I have a collection Playlist that contains an array of items
{
userId: {
type : String,
required : true,
index : true,
unique : true
},
items: [
{
id: { // do not mix up with _id, which is the autogenerated id of the pair {id,type}. ID is itemId
type : Schema.Types.ObjectId
},
type: {
type : String
}
}
]
}
Mongo automatically adds the _id field to the items when I push a pair {id,type} to items (but I don't care about it).
Now I would like to remove several "pairs" at once from the items array.
I have tried using $pullAll but it requires an exact match, and I do not know the _id, so it does not remove anything from items
playlistModel.update({userId:userId},{$pullAll:{items:[{id:"123",type:"video"},{id:"456",type:"video"}]}},null,function(err){
I have tried using $pull with different variants, but it removed ALL objects from items
playlistModel.update({userId:userId},{$pull:{items:{"items.id":{$in:["123","456"]}}}},null,function(err){
playlistModel.update({userId:userId},{$pull:{items:{$in:[{id:"123",type:"video"},{id:"456",type:"video"}]}}},null,function(err){
Am I missing something or am I asking something that isn't implemented?
If the latter, is there a way I can go around that _id issue?
OK I found a way that works using $pull:
playlistModel.update({userId:userId},{$pull:{items:{id:{$in:["123","456"]}}}},null,function(err){
It doesn't take the type into account but I can't see any issue with that since the id is unique across all types anyway.
Although I will wait a bit to see if someone has a better solution to offer
EDIT
With Veeram's help I got to this other solution, which IMO is more elegant because I don't have _ids that I don't need in the database, and the $pullAll option seems more correct here
var playlistItemSchema = mongoose.Schema({
id: { // do not mix up with autogenerated _id. id is itemId
type : Schema.Types.ObjectId
},
type: {
type : String
}
},{ _id : false });
var schema = new Schema({
userId: {
type : String,
required : true,
index : true,
unique : true
},
items: [playlistItemSchema]
});
playlistModel.update({userId:userId},{$pullAll:{items:[{id:"123",type:"video"},{id:"456",type:"video"}]}},null,function(err){
tips:
you can use _id field to handle your playlistModel data.
mongoose api : new mongoose.Types.ObjectId to generate an Object_id
let _id=new mongoose.Types.ObjectId;
playlistModel.updateMany({_id:_id},{ $set: { name: 'bob' }}).exec(data=>{console.log('exec OK')});

Resources