-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathnodejs-documents-query-sort.js
More file actions
133 lines (125 loc) · 4.44 KB
/
Copy pathnodejs-documents-query-sort.js
File metadata and controls
133 lines (125 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* Copyright (c) 2015-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
*/
var should = require('should');
var testconfig = require('../etc/test-config-qa.js');
var marklogic = require('../');
var q = marklogic.queryBuilder;
var db = marklogic.createDatabaseClient(testconfig.restReaderConnection);
var dbWriter = marklogic.createDatabaseClient(testconfig.restWriterConnection);
var dbAdmin = marklogic.createDatabaseClient(testconfig.restAdminConnection);
describe('document query sort test', function () {
before(function (done) {
this.timeout(10000);
// NOTE: must create a string range index on rangeKey1 and rangeKey2
dbWriter.documents.write({
uri: '/test/query/sort/doc1.json',
collections: ['matchCollection1'],
contentType: 'application/json',
content: {
title: 'Vannevar Bush',
popularity: 5,
id: '0011',
date: '2005-01-01',
price: {
amt: 0.1
},
p: 'Vannevar Bush wrote an article for The Atlantic Monthly'
}
}, {
uri: '/test/query/sort/doc2.json',
collections: ['matchCollection1', 'matchCollection2'],
contentType: 'application/json',
content: {
title: 'The Bush article',
popularity: 4,
id: '0012',
date: '2006-02-02',
price: {
amt: 0.12
},
p: 'The Bush article described a device called a Memex'
}
}, {
uri: '/test/query/sort/doc3.json',
collections: ['matchCollection2'],
contentType: 'application/json',
content: {
title: 'For 1945',
popularity: 3,
id: '0013',
date: '2007-03-03',
price: {
amt: 1.23
},
p: 'For 1945, the thoughts expressed in the Atlantic Monthly were groundbreaking'
}
}, {
uri: '/test/query/sort/doc4.json',
collections: [],
contentType: 'application/json',
content: {
title: 'Vannevar served',
popularity: 5,
id: '0024',
date: '2008-04-04',
price: {
amt: 12.34
},
p: 'Vannevar served as a prominent policymaker and public intellectual'
}
}, {
uri: '/test/query/sort/doc5.json',
collections: ['matchList'],
contentType: 'application/json',
content: {
title: 'The memex',
popularity: 5,
id: '0026',
date: '2009-05-05',
price: {
amt: 123.45
},
p: 'The Memex, unfortunately, had no automated search feature'
}
}).
result(function (response) {
done();
}, done);
});
it('should do document query with descending order', function (done) {
db.documents.query(
q.where(
q.directory('/test/query/sort/')
).
orderBy(q.sort('popularity', 'descending'))
).result(function (response) {
var document = response[0];
response.length.should.equal(5);
//console.log(JSON.stringify(response, null, 4));
document.content.popularity.should.equal(5);
done();
}, done);
});
it('should do document query with ascending order', function (done) {
db.documents.query(
q.where(
q.directory('/test/query/sort/')
).
orderBy(q.sort('popularity', 'ascending'))
).result(function (response) {
var document = response[0];
response.length.should.equal(5);
//console.log(JSON.stringify(response, null, 4));
document.content.popularity.should.equal(3);
done();
}, done);
});
it('should remove all documents', function (done) {
dbAdmin.documents.removeAll({ all: true }).
result(function (response) {
response.should.be.ok;
done();
}, done);
});
});